8.使用GDI+画图
1.DrawIcon
DrawIcon(Icon icon, Rectangle targetRect) DrawIcon(Icon icon, int x, int y) DrawIconUnstretched(Icon icon, Rectangle targetRect)
private void Form1_Paint(object sender, PaintEventArgs e) { //创建画板从Paint事件中的直接引用Graphics对象 Graphics graphics = e.Graphics; graphics.Clear(Color.Black); //创建一个Ico Icon newIcon = new Icon("SampIcon.ico"); Rectangle rect = new Rectangle(20, 20, 50, 50); e.Graphics.DrawIcon(newIcon, rect); e.Graphics.DrawIcon(newIcon, 80, 20); //宽度和高度不会对原图照成影响 Rectangle rect2 = new Rectangle(120, 20, 100, 100); e.Graphics.DrawIconUnstretched(newIcon, rect2); }
2.DrawImage
DrawImage 方法 (Image, Point)
DrawImage 方法 (Image, Int32, Int32)
DrawImage 方法 (Image, PointF)
DrawImage 方法 (Image, Single, Single)
DrawImage 方法 (Image, Point[])
DrawImage 方法 (Image, PointF[])
private void Form1_Paint(object sender, PaintEventArgs e) { Image newImage = Image.FromFile("SampImag.png"); Point ulCorner = new Point(100, 100); e.Graphics.DrawImage(newImage, ulCorner); }
private void Form1_Paint(object sender, PaintEventArgs e) { Image newImage = Image.FromFile("SampImag.png"); Point ulCorner = new Point(100, 100); Point urCorner = new Point(550, 100); Point llCorner = new Point(150, 250); Point[] destPara = { ulCorner, urCorner, llCorner }; //Points 参数指定平行四边形的三个点。 三个 Point 结构表示平行四边形的左上角、右上角和左下角。 //可从前三个点推断出第四个点以形成一个平行四边形。 //缩放和剪切 image 参数表示的图像,以适合 Points参数指定的平行四边形的形状。 e.Graphics.DrawImage(newImage, destPara); }
DrawImage 方法 (Image, Rectangle)
DrawImage 方法 (Image, Int32, Int32, Int32, Int32)
DrawImage 方法 (Image, RectangleF)
DrawImage 方法 (Image, Single, Single, Single, Single)
private void Form1_Paint(object sender, PaintEventArgs e) { Image newImage = Image.FromFile("SampImag.png"); Rectangle destRect = new Rectangle(100, 100, 450, 150); // 由 image 对象表示的图像被缩放为 rect 矩形的尺寸 e.Graphics.DrawImage(newImage, destRect); }
//按照第三个参数所指定从图片中取出一个矩形部分,然后将这个矩形部分放到第二个参数指定的部分来显示 //第二个参数可以是Point[]的平行四边形,也可以是Rectangle的矩形。 //第四个参数用于控制图形绘制单位 DrawImage 方法 (Image, Point[], Rectangle, GraphicsUnit) DrawImage 方法 (Image, PointF[], RectangleF, GraphicsUnit) DrawImage 方法 (Image, Rectangle, Rectangle, GraphicsUnit) DrawImage 方法 (Image, RectangleF, RectangleF, GraphicsUnit) //下面的是用int或folat,是在指定的位置绘制图像的一部分,上面的是在指定的区域 DrawImage 方法 (Image, Int32, Int32, Rectangle, GraphicsUnit) DrawImage 方法 (Image, Single, Single, RectangleF, GraphicsUnit)
private void Form1_Paint(object sender, PaintEventArgs e) { Image newImage = Image.FromFile("SampImag.png"); Point ulCorner = new Point(100, 100); Point urCorner = new Point(325, 100); Point llCorner = new Point(150, 250); Point[] destPara = { ulCorner, urCorner, llCorner }; Rectangle srcRect = new Rectangle(50, 50, 150, 150); GraphicsUnit units = GraphicsUnit.Pixel; e.Graphics.DrawImage(newImage, destPara, srcRect, units); }
DrawImage 方法 (Image, Point[], Rectangle, GraphicsUnit, ImageAttributes)
DrawImage 方法 (Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes)
ImageAttributes ,它指定 image 对象的重新着色和伽玛信息。
private void Form1_Paint(object sender, PaintEventArgs e) { Image newImage = Image.FromFile("SampImag.png"); Rectangle srcRect = new Rectangle(50, 50, 150, 150); GraphicsUnit units = GraphicsUnit.Pixel; Point ulCorner2 = new Point(325, 100); Point urCorner2 = new Point(550, 100); Point llCorner2 = new Point(375, 250); Point[] destPara2 = { ulCorner2, urCorner2, llCorner2 }; ImageAttributes imageAttr = new ImageAttributes(); imageAttr.SetGamma(4.0F); e.Graphics.DrawImage(newImage, destPara2, srcRect, units, imageAttr); }