Grapgics对象:
Grapgics对象类似画布或者一张纸,供其他对象在上面作图。
//---------------------------------------------------
c#创建Graphics对象的三种方法
方法一、利用控件或窗体的Paint事件中的PainEventArgs
在窗体或控件的Paint事件中接收对图形对象的引用,作为PaintEventArgs(PaintEventArgs指定绘制控件所用的Graphics)的一部分,在为控件创建绘制代码时,通常会使用此方法来获取对图形对象的引用。
例如:
//窗体的Paint事件的响应方法
复制代码代码如下:
private void form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
}
也可以直接重载控件或窗体的OnPaint方法,具体代码如下所示:
复制代码代码如下:
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
}
Paint事件在重绘控件时发生。
方法二、调用某控件或窗体的CreateGraphics方法
调用某控件或窗体的CreateGraphics方法以获取对Graphics对象的引用,该对象表示该控件或窗体的绘图图面。如果想在已存在的窗体或控件上绘图,通常会使用此方法。
例如:
Graphics g = this.CreateGraphics();
方法三、调用Graphics类的FromImage静态方法
由从Image继承的任何对象创建Graphics对象。在需要更改已存在的图像时,通常会使用此方法。
例如:
复制代码代码如下:
//名为“g1.jpg”的图片位于当前路径下
Image img = Image.FromFile("g1.jpg");//建立Image对象
Graphics g = Graphics.FromImage(img);//创建Graphics对象
//---------------------------------------------------
Graphics平移缩放旋转
【平移】
private void btnTranslate_Click(object sender, EventArgs e)
{
Graphics graphics = this.CreateGraphics();
// 红色笔
Pen pen = new Pen(Color.Red, 5);
Rectangle rect = new Rectangle(0, 0, 200, 50);
// 用红色笔画矩形
graphics.DrawRectangle(pen, rect);
// 向左平移100向下平移50
graphics.TranslateTransform(100,50);
// 蓝色笔
pen.Color = Color.Blue;
// 用蓝色笔重新画平移之后的矩形
graphics.DrawRectangle(pen, rect);
graphics.Dispose();
pen.Dispose();
}
【缩放】
private void btnScale_Click(object sender, EventArgs e)
{
Graphics graphics = this.CreateGraphics();
// 红色笔
Pen pen = new Pen(Color.Red, 5);
Rectangle rect = new Rectangle(0, 0, 200, 50);
// 用红色笔画矩形
graphics.DrawRectangle(pen, rect);
graphics.ScaleTransform(0.5f, 2);
// 蓝色笔
pen.Color = Color.Blue;
// 用蓝色笔重新画平移之后的矩形
graphics.DrawRectangle(pen, rect);
graphics.Dispose();
pen.Dispose();
}
宽缩小一半,高放大一倍
【旋转】
private void btnTraslate_Click(object sender, EventArgs e)
{
Graphics graphics = this.CreateGraphics();
// 红色笔
Pen pen = new Pen(Color.Red, 5);
Rectangle rect = new Rectangle(0, 0, 200, 50);
// 用红色笔画矩形
graphics.DrawRectangle(pen, rect);
graphics.TranslateTransform(200,0);
graphics.RotateTransform(90);
// 蓝色笔
pen.Color = Color.Blue;
// 用蓝色笔重新画平移之后的矩形
graphics.DrawRectangle(pen, rect);
graphics.Dispose();
pen.Dispose();
}
坐标原点为矩形的左上点。
//---------------------------------------------------
看到網友發問,自己隨手記錄一下。
一般合併圖片大多情況有三種,水平、垂直、浮水印,這裡簡單實做一下。
來源兩張圖片
水平合併
private Image HorizontalMergeImages(Image img1, Image img2)
{
Image MergedImage = default(Image);
Int32 Wide = 0;
Int32 High = 0;
Wide = img1.Width + img2.Width;//設定寬度
if (img1.Height >= img2.Height)
{
High = img1.Height;
}
else
{
High = img2.Height;
}
Bitmap mybmp = new Bitmap(Wide, High);
Graphics gr = Graphics.FromImage(mybmp);
//處理第一張圖片
gr.DrawImage(img1, 0, 0);
//處理第二張圖片
gr.DrawImage(img2, img1.Width, 0);
MergedImage = mybmp;
gr.Dispose();
return MergedImage;
}
結果
垂直合併
private Image HorizontalMergeImages(Image img1, Image img2)
{
Image MergedImage = default(Image);
Int32 Wide = 0;
Int32 High = 0;
High = img1.Height + img2.Height;//設定高度
if (img1.Width >= img2.Width)
{
Wide = img1.Width;
}
else
{
Wide = img2.Width;
}
Bitmap mybmp = new Bitmap(Wide, High);
Graphics gr = Graphics.FromImage(mybmp);
//處理第一張圖片
gr.DrawImage(img1, 0, 0);
//處理第二張圖片
gr.DrawImage(img2, 0, img1.Height);
MergedImage = mybmp;
gr.Dispose();
return MergedImage;
}
結果
圖片浮水印
private Image MarkImage(Image img1, Image img2)
{
Image MergedImage = default(Image);
//設定背景圖片
Graphics gr = System.Drawing.Graphics.FromImage(img1);
//新建logo浮水印圖片
Bitmap Logo = new Bitmap(img2.Width, img2.Height);
Graphics tgr = Graphics.FromImage(Logo);
ColorMatrix cmatrix = new ColorMatrix();
//設定圖片色彩(透明度)
cmatrix.Matrix33 = 0.5F;
ImageAttributes imgattributes = new ImageAttributes();
imgattributes.SetColorMatrix(cmatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
tgr.DrawImage(img2, new Rectangle(0, 0, Logo.Width, Logo.Height), 0, 0, Logo.Width, Logo.Height, GraphicsUnit.Pixel, imgattributes);
tgr.Dispose();
//logo圖片位置
gr.DrawImage(Logo, img1.Width/3,10);
gr.Dispose();
MergedImage = img1;
return MergedImage;
}
結果