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;
}
}
}
分享到:
相关推荐
《网店商品图片与视频拍摄及处理》—08商品图片处理.pdf《网店商品图片与视频拍摄及处理》—08商品图片处理.pdf《网店商品图片与视频拍摄及处理》—08商品图片处理.pdf《网店商品图片与视频拍摄及处理》—08商品图片...
SimpleImage是阿里巴巴的一个Java图片处理的类库,可以实现图片缩略、水印等处理。 SimpleImage中的ImageRender是图片处理的基类,它是一个抽象类,我们看到,该类中定义了一个抽象方法render(),同时持有一个对...
图片处理工具图片处理工具图片处理工具图片处理工具图片处理工具图片处理工具图片处理工具图片处理工具图片处理工具图片处理工具图片处理工具图片处理工具图片处理工具
为淘客专门做的,为你带来皇冠、、、美图淘淘是一款专为网店卖家设计的批量图片处理软件,包括图片处理、添加装饰素材、边框、文字等图片处理的常用功能。美图淘淘可同时对多张图片进行处理,将原先需要重复劳动的...
VC图片处理,具体请看代码!很简单的 VC图片处理,具体请看代码!很简单的 VC图片处理,具体请看代码!很简单的
是本人整理的所有图片处理的代码,包括 图片尺寸 大小 等比缩放 加水印 ....
PHP图片处理(上传、加水印、缩略图),对图片的处理
我的图片处理我的图片处理我的图片处理我的图片处理我的图片处理我的图片处理我的图片处理
Android图片处理效果demo, 圆角,倒影
一款图片处理的超级牛的软件! 可以化腐朽为神奇!!!试一试就知道!!!
这个里面是图片处理系列的一个集合demo,里面包含了图片处理系列的所有代码,写的比较乱,没有整理,希望对大家有帮助。
php 图片处理类 图片上传 缩略图 实用的 直接可用
Java图片处理工具ImageMagick
vb图片处理vb图片处理vb图片处理vb图片处理vb图片处理vb图片处理
linux下图片处理工具
Image batch dispose批量图片处理工具,支持对批量图片的加文字,批量改变图片大小,批量更名,可以批量旋转图片,可以批量压缩图片文件大小,可以批量锐化图片,批量转换图片格式(bmp,jpg,gif,png,.tiff等)为Jpg格式....
C#图片处理工具,可以实现对图片的曝光度、灰度、柔化、锐度的处理。
一款简单实用的JS拖动滑块对比图片处理前后效果代码,通过拖动滑块来将原图和处理后的图片两张图片进行对比。
php gd库图片处理类,添加水印,旋转图片,
一个简单的图片处理程序,可以实现图片的翻转变化,大小改变和像素改变,使用了Qt里面的多线程技术。