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;
}
}
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;
}
}
相关文章推荐
- MFC GDI+实现以鼠标为中心缩放图片(并且可以拖动)
- c# 鼠标拖动缩放图片
- c# 鼠标拖动缩放图片
- MFC GDI+实现以鼠标为中心缩放图片(并且可以拖动)
- c# WPF 中图片缩放功能,鼠标拖动位置
- C#实现图片鼠标拖动和滚动缩放
- C#实现图片鼠标拖动和滚动缩放
- PNG透明图片叠加(Opencv实现,包括旋转缩放以及边界处理)
- MFC GDI+实现以鼠标为中心缩放图片(并且可以拖动)
- Android 图片旋转,缩放,切割,叠加处理
- C# winfrom缩放图片并且可以鼠标拖动
- Android 图片旋转,缩放,切割,叠加处理
- C# 处理图片拖动和缩放功能
- 上传图片的缩放处理
- C#图片处理之:图片缩放和剪裁
- android 图片处理(截取,缩放)
- 黄聪:C#图片处理封装类(裁剪、缩放、清晰度、加水印、生成缩略图)有示例(转)
- Struts2 图片上传、缩放、剪切处理 第一节
- 安卓中关于图片从网络获取,压缩,上传,下载,缩略图,缓存的一些处理总结(一)
- [unity3d]鼠标拖动and旋转缩放