`
isiqi
  • 浏览: 16039920 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

图片处理

阅读更多

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Windows1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 16);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "打开图";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 200);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(416, 120);
this.label1.TabIndex = 1;
//
// button2
//
this.button2.Location = new System.Drawing.Point(176, 16);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "生成";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(8, 48);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(424, 136);
this.pictureBox1.TabIndex = 3;
this.pictureBox1.TabStop = false;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(472, 333);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
openFileDialog1.ShowDialog();
pictureBox1.Image=System.Drawing.Bitmap.FromFile(openFileDialog1.FileName);
}

private void button2_Click(object sender, System.EventArgs e)
{
byte[] byte_img=pic_zero(openFileDialog1.FileName,100,150);
label1.Text=byte_img.Length.ToString();
}
/// <summary>
/// byte[] 图片另存
/// </summary>
/// <param name="sourcepath"></param>
/// <param name="kuan"></param>
/// <param name="gao"></param>
/// <returns></returns>
public byte[] pic_zero(string sourcepath,int kuan,int gao)
{
byte[] byte_img=null;
try
{
//生成流
System.IO.Stream stream=new System.IO.MemoryStream();
string originalFilename =sourcepath;
//生成的高质量图片名称
//从文件取得图片对象
System.Drawing.Image image = System.Drawing.Image.FromFile(originalFilename);
int iImgWidth = image.Width;
int iImgHeight = image.Height;
//int iScale = (iImgWidth / scale)>(iImgHeight/scale) ? (iImgWidth / scale) : (iImgHeight / scale);

//取得图片大小
System.Drawing.Size size = new Size(kuan ,gao);
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width,size.Height);

//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空一下画布
g.Clear(Color.Blue);
//在指定位置画图
g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),new System.Drawing.Rectangle(0, 0, image.Width,image.Height),System.Drawing.GraphicsUnit.Pixel);
//保存高清晰度的缩略图
//bitmap.Save(strGoodFile, System.Drawing.Imaging.ImageFormat.Bmp);
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte_img=ReadImage(stream);
g.Dispose();
image.Dispose();
bitmap.Dispose();

}
catch(Exception e)
{
MessageBox.Show("优化图片错误!"+e.ToString(),"错误");
}
return byte_img;
}

/// <summary>
/// 从给定的流中读取数据到一个字节数组中,并返回此数组。
/// 如果给定的流不是一个图像格式的流,将报异常。
/// 返回的字节数组中,将非BMP和JEPG格式的图像数据流转换为JEPG格式输出,以支持大多数应用。
/// 适用于直接从数据库中读取的二进制图像流的处理。
/// </summary>
/// <param name="stream">给定的图像数据流。</param>
/// <returns>从流中读取的数据。</returns>
public static byte[] ReadImage(System.IO.Stream stream)
{
Image image = Image.FromStream(stream);
byte[] myImage = null;

if(image.RawFormat.Guid != System.Drawing.Imaging.ImageFormat.Jpeg.Guid && image.RawFormat.Guid != System.Drawing.Imaging.ImageFormat.Bmp.Guid)
{
System.IO.MemoryStream memStream = new System.IO.MemoryStream();
image.Save(memStream, System.Drawing.Imaging.ImageFormat.Jpeg);
myImage = memStream.GetBuffer();
memStream.Close();
}
else
{
stream.Position = 0;
myImage = new byte[stream.Length];
stream.Read(myImage, 0, (int)stream.Length);
}
return myImage;
}
}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics