winform自定义控件(gdi+)(11)——绘制图像(DrawImage)

1、显示图像

 Graphics g = e.Graphics;
            Bitmap bmp = new Bitmap("1.jpg");
            g.DrawImage(bmp,0,0);//屏幕上图像的大小为屏幕分辨率和图像分辨率的比值



            Console.WriteLine(g.DpiX );
            Console.WriteLine(g.DpiY );
            Console.WriteLine(bmp.HorizontalResolution);
            Console.WriteLine(bmp.VerticalResolution );


            //为了显示和原始图像一样物理尺寸的大小,又由于屏幕分辨率比较低,所以图像的分辨率实际上降低了
            Console.WriteLine($"图像像素{bmp.Size}" );
            SizeF s = new SizeF(bmp.Width *(g.DpiX /bmp.HorizontalResolution ),bmp.Height *(g.DpiY /bmp.VerticalResolution ));
            Console.WriteLine($"屏幕图像像素{s}");




            Rectangle r = new Rectangle(0, 100, bmp.Width, bmp.Height);
            Rectangle sourceR = new Rectangle(0, 0, bmp.Width, bmp.Height);
            g.DrawImage(bmp, r, sourceR, GraphicsUnit.Pixel);//源矩形和目标矩形大小相同时,就不进行图像的缩放,目标矩形大于原始矩形则放大,否则缩小

输出:
winform自定义控件(gdi+)(11)——绘制图像(DrawImage)
2、改变图像分辨率对图像显示的影响

   Graphics g = e.Graphics;
    Bitmap bmp = new Bitmap("1.jpg");
    g.DrawImage(bmp,0,0);//屏幕上图像的大小为屏幕分辨率和图像分辨率的比值

    bmp.SetResolution(600,600);
    g.DrawImage(bmp, 200, 0);

    bmp.SetResolution(1200, 1200);
    g.DrawImage(bmp, 400, 0);

输出:
winform自定义控件(gdi+)(11)——绘制图像(DrawImage)
说明对图像尺寸的影响等于图像宽度或高度(单位像素)/图像分辨率或高度,由于分辨率的增加,所以尺寸减小。

3、使用插值算法对图像进行缩放

  Graphics g = e.Graphics;
            Bitmap bmp = new Bitmap("1.jpg");
            g.DrawImage(bmp,0,0);//屏幕上图像的大小为屏幕分辨率和图像分辨率的比值

            g.InterpolationMode = InterpolationMode.NearestNeighbor;
            Rectangle r = new Rectangle(0, 100, bmp.Width, bmp.Height);
            Rectangle sourceR = new Rectangle(0, 0, bmp.Width, bmp.Height);
            g.DrawImage(bmp, r, sourceR, GraphicsUnit.Pixel);//源矩形和目标矩形大小相同时,就不进行图像的缩放,目标矩形大于原始矩形则放大,否则缩小
           
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            r = new Rectangle(500, 100, bmp.Width, bmp.Height);
            g.DrawImage(bmp, r, sourceR, GraphicsUnit.Pixel);

4、图像裁剪

  Graphics g = e.Graphics;
            Bitmap bmp = new Bitmap("1.jpg");
            g.DrawImage(bmp,0,0);//屏幕上图像的大小为屏幕分辨率和图像分辨率的比值

        
            Rectangle r = new Rectangle(0, 100, bmp.Width, bmp.Height);
            Rectangle sourceR = new Rectangle(100, 100, bmp.Width-100, bmp.Height-100);
            g.DrawImage(bmp, r, sourceR, GraphicsUnit.Pixel);//源矩形和目标矩形大小相同时,就不进行图像的缩放,目标矩形大于原始矩形则放大,否则缩小
           

5、图像的变形(仿射变换)
代码:

``csharp
 Graphics g = e.Graphics;
            Bitmap bmp = new Bitmap("1.jpg");
            Point[] points = {new Point (0,0),new Point (100,0),new Point (50,100) };
            g.DrawImage(bmp, points);

输出:
winform自定义控件(gdi+)(11)——绘制图像(DrawImage)
说明:
points中的三个点分别代表了左上角、右上角、左下角的三个点

6、图像翻转(上下对折)
代码:

Graphics g = e.Graphics;
            Bitmap bmp = new Bitmap("1.jpg");
            Point[] points = {new Point (0,100),new Point (100,100),new Point (0,0) };
            g.DrawImage(bmp, points);

输出:
winform自定义控件(gdi+)(11)——绘制图像(DrawImage)

7、旋转

 Graphics g = e.Graphics;
            Bitmap bmp = new Bitmap("1.jpg");
            Point[] points = {new Point (100,0),new Point (100,100),new Point (0,0) };
            g.DrawImage(bmp, points);

输出:
winform自定义控件(gdi+)(11)——绘制图像(DrawImage)
说明:
顺时针旋转90度
8、复制图像
代码:

Graphics g = e.Graphics;
            Bitmap bmp = new Bitmap("1.jpg");

            Bitmap bmp2 = bmp.Clone(new Rectangle(50, 50, bmp.Width - 50, bmp.Height - 50), PixelFormat.DontCare);
       
            g.DrawImage(bmp,0,0);
            g.DrawImage(bmp2,200,0);

输出:
winform自定义控件(gdi+)(11)——绘制图像(DrawImage)
9、在内存中创建位图
代码:

Graphics g = e.Graphics;
            Bitmap bmp = new Bitmap(100,100);
            Graphics gImg = Graphics.FromImage(bmp );
            gImg.FillRectangle(Brushes .Red ,new RectangleF (0,0,50,50));
            gImg.DrawRectangle(Pens .Black  , new Rectangle(0, 0, 50, 50));

            g.DrawImage(bmp,0,0);
            

输出:
winform自定义控件(gdi+)(11)——绘制图像(DrawImage)

上一篇:Multi-Task Multi-Sensor Fusion for 3D Object Detection


下一篇:报表和仪表板生成器Stimulsoft v2021.1最新版来啦!