您的位置:首页 > 编程语言 > C#

C#图片处理总结——叠加、缩放、鼠标拖动

2011-05-03 12:37 489 查看
/////////////用到的命名空间////////////

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.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);
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;
}
}

//////////////鼠标拖动图片/////////////

/////////////定义图片位置,全局变量////////////

Point M_pot_p = new Point();//原始位置
int M_int_mx = 0, M_int_my = 0;//下次能继续
int M_int_maxX, M_int_maxY;//加快读取用

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Bitmap tmpbmp = (Bitmap)this.pictureBox1.Image;
M_pot_p = e.Location;
M_int_maxX = pictureBox1.Width - tmpbmp.Width;
M_int_maxY = pictureBox1.Height - tmpbmp.Height;
Cursor = Cursors.SizeAll;
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)//当按左键的时候
{
//算差值
Bitmap tmpbmp = (Bitmap)this.pictureBox1.Image;
M_int_mx = M_int_mx - M_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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: