第一篇:C#图片处理总结——叠加、缩放、鼠标拖动
C#图片处理总结——叠加、缩放、鼠标拖动
2011-05-03 12:37 1265人阅读 评论(2)收藏 举报
c#floatimageobjectstringnull
/////////////用到的命名空间////////////
using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imaging;
/////两张图片叠加,float fImage(0—1)透明度///////////////
private void getMixImage(float fImage, string strFrontImage, string strBackImage){ this.m_image.Dispose();Bitmap background = new Bitmap(strBackImage);Bitmap frontImage = new Bitmap(strFrontImage);int iwidth = background.Width > frontImage.Width ? background.Width : frontImage.Width;int iheight = background.Height > frontImage.Height ? background.Height : frontImage.Height;Bitmap mixImage2 = new Bitmap(iwidth, iheight);//this.mixImage.Width = iwidth;//this.mixImage.Height = iheight;Graphics g = Graphics.FromImage(mixImage2);float[][] colormatrix ={ new float[]{1,0,0,0,0},//代表了R new float[]{0,1,0,0,0},//代表了G new float[]{0,0,1,0,0},//代表了B new float[]{0,0,0,fImage,0},//代表了A new float[]{0,0,0,0,1} };ColorMatrix cm = new ColorMatrix(colormatrix);ImageAttributes imageAtt = new ImageAttributes();imageAtt.SetColorMatrix(cm, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);g.DrawImage(background, new Point(0, 0));g.DrawImage(frontImage, new Rectangle(0, 0, frontImage.Width, frontImage.Height), 0, 0, frontImage.Width, frontImage.Height, GraphicsUnit.Pixel, imageAtt);this.pictureBox1.Image = mixImage2;this.pictureBox1.Update();this.m_ChangeSize = mixImage2;}
//////////////图片的缩放/////////////
public Form1(){ InitializeComponent();/////////////写在初始化时,提高运算速度////////////
base.SetStyle(ControlStyles.OptimizedDoubleBuffer |ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);base.SetStyle(ControlStyles.ResizeRedraw | ControlStyles.Selectable, true);} /////////////放大////////////
private void buttonPicSizeEnlarge_Click(object sender, EventArgs e){ this.nPicSize++;if(this.nPicSize < 1){ this.nPicSize = 1;MessageBox.Show(“无法再减弱”);} if(this.nPicSize > 6){ MessageBox.Show(“无法再增强”);this.nPicSize = 6;} else {
this.picSizeColor();if(this.nReset == 0)m_image = m_Reset;if(this.pictureBox1.Image.Width > this.splitContainer1.Panel1.Width && this.pictureBox1.Image.Height > this.splitContainer1.Panel1.Height && pictureBox1.Image.Height >= m_image.Height * 4){ MessageBox.Show(“已是最大”);} else { this.nReset = this.nReset + 1;Rectangle oldrct;Bitmap bmp =(Bitmap)this.pictureBox1.Image;oldrct = new Rectangle(0, 0, bmp.Width, bmp.Height);this.pictureBox1.Image = bmp;Bitmap tmpbmp = null;tmpbmp = new Bitmap(bmp.Width * 2, bmp.Height * 2);if(bmp.Width < m_image.Width){ oldrct = new Rectangle(0, 0, m_image.Width, m_image.Height);Graphics g = Graphics.FromImage(tmpbmp);Rectangle newrct = new Rectangle(0, 0, tmpbmp.Width, tmpbmp.Height);g.DrawImage(m_image, newrct, oldrct, GraphicsUnit.Pixel);g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;pictureBox1.Image = tmpbmp;g.Dispose();pictureBox1.Update();} else { Graphics g = Graphics.FromImage(tmpbmp);Rectangle newrct = new Rectangle(0, 0, tmpbmp.Width, tmpbmp.Height);g.DrawImage(bmp, newrct, oldrct, GraphicsUnit.Pixel);g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;pictureBox1.Image = tmpbmp;g.Dispose();pictureBox1.Update();} } this.m_image =(Bitmap)this.pictureBox1.Image;} }
/////////////缩小////////////
private void buttonPicSizeNarrow_Click(object sender, EventArgs e){ this.nPicSize--;if(this.nPicSize < 1){ this.nPicSize = 1;MessageBox.Show(“无法再缩小”);} if(this.nPicSize > 6){ MessageBox.Show(“无法再增大”);this.nPicSize = 6;} else { this.picSizeColor();if(this.nReset == 0)m_image = m_Reset;if(pictureBox1.Image.Width <= this.splitContainer1.Panel1.Width && pictureBox1.Image.Height <= this.splitContainer1.Panel1.Height){ MessageBox.Show(“已经最小”);} else { this.nReset = this.nResettmpbmp.Width;M_int_maxY = pictureBox1.HeightM_pot_p.X + e.X;M_int_my = M_int_my-M_pot_p.Y + e.Y;//锁定范围
M_int_mx = Math.Min(0, Math.Max(M_int_maxX, M_int_mx));M_int_my = Math.Min(0, Math.Max(M_int_maxY, M_int_my));Graphics g = pictureBox1.CreateGraphics();g.DrawImage(tmpbmp, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height), new Rectangle(-M_int_mx,-M_int_my, pictureBox1.Width, pictureBox1.Height), GraphicsUnit.Pixel);M_pot_p = e.Location;} else { Cursor = Cursors.Default;} }