GDI+入门(四、GDI+入门 简单的图像处理)

 

四、GDI+入门 简单的图像处理

private void button1_Click(object sender, EventArgs e)

        {

            Graphics g=this.CreateGraphics();

            Bitmap bmp = new Bitmap("1.jpg");

            g.DrawImage(bmp, 0, 0);

        }

 

        private void Form1_Paint(object sender, PaintEventArgs e)

        {

            SetStyle(ControlStyles.DoubleBuffer, true);

            SetStyle(ControlStyles.AllPaintingInWmPaint, true);

            SetStyle(ControlStyles.UserPaint, true);

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            ImageCodecInfo[] acailableCodes;

            acailableCodes = ImageCodecInfo.GetImageEncoders();

            int numCodecs = acailableCodes.Length;

            for (int i = 0; i < numCodecs; i++)

            {

                Console.WriteLine("Codecs Name=" + acailableCodes[i].CodecName);

                Console.WriteLine("Class ID=" + acailableCodes[i].Clsid.ToString());

                Console.WriteLine("Filename Extension=" + acailableCodes[i].FilenameExtension);

                Console.WriteLine("Flags=" + acailableCodes[i].Flags.ToString());

                Console.WriteLine("Format Desctiption=" + acailableCodes[i].FormatDescription);

                Console.WriteLine("Format ID=" + acailableCodes[i].FormatID.ToString());

                Console.WriteLine("MimeType=" + acailableCodes[i].MimeType);

                Console.WriteLine("Version=" + acailableCodes[i].Version.ToString());

                Console.WriteLine();

            }

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

            Bitmap bmp1 = new Bitmap(100, 100, PixelFormat.Format32bppArgb);

            Bitmap bmp2 = new Bitmap(100, 100, PixelFormat.Format24bppRgb);

 

            bool b1 = ((bmp1.PixelFormat & PixelFormat.Alpha) != 0);

            bool b2 = ((bmp2.PixelFormat & PixelFormat.Alpha) != 0);

            Console.WriteLine("bmp1 has alpha?:" + b1);

            Console.WriteLine("bmp2 has alpha?:" + b2);

        }

 

        private void button4_Click(object sender, EventArgs e)

        {

            Graphics g = this.CreateGraphics();

            Bitmap bmp = new Bitmap("1.jpg");

            g.DrawImage(bmp, 0, 0);

            Console.WriteLine("Screen resolution:" + g.DpiX + "DPI");

            Console.WriteLine("Image resolution:" + bmp.HorizontalResolution + "DPI");

            Console.WriteLine("Image With:" + bmp.Width);

            Console.WriteLine("Image Height:" + bmp.Height);

            SizeF s = new SizeF(bmp.Width * (g.DpiX / bmp.HorizontalResolution), bmp.Height * (g.DpiY / bmp.VerticalResolution));

            Console.WriteLine("Display size of image:" + s);

        }

        private void button6_Click(object sender, EventArgs e)

        {

            Graphics g = this.CreateGraphics();

            Bitmap bmp = new Bitmap("1.jpg");

            g.DrawImage(bmp, this.ClientRectangle);

            g.Dispose();

            bmp.Dispose();

        }

 

        private void button7_Click(object sender, EventArgs e)

        {

            Graphics g = this.CreateGraphics();

            Bitmap bmp = new Bitmap("1.jpg");

          //  g.FillRectangle(Brushes.White, this.ClientRectangle);

            bmp.SetResolution(600f, 600f);

            g.DrawImage(bmp, 0, 0);

            bmp.SetResolution(1200f, 1200f);

            g.DrawImage(bmp, 180, 0);

            g.Dispose();

        }

 

        private void button8_Click(object sender, EventArgs e)

        {

            Graphics g = this.CreateGraphics();

            Bitmap bmp = new Bitmap("1.jpg");

            bmp.SetResolution(50f, 50f);

            Rectangle r0 = new Rectangle(0, 0, bmp.Width, bmp.Height);

            Rectangle r = new Rectangle(0, 0, (int)(bmp.Width * (bmp.Width / g.DpiX)), (int)(bmp.Height * (bmp.Height / g.DpiY)));

           // g.FillRectangle(Brushes.Red, r);

            g.DrawImage(bmp, r, r0, GraphicsUnit.Pixel);

        }

 

        private void button9_Click(object sender, EventArgs e)

        {

            Graphics g = this.CreateGraphics();

            Bitmap bmp = new Bitmap("1.jpg");

          //  g.FillRectangle(Brushes.Blue, this.ClientRectangle);

            int width = bmp.Width;

            int height = bmp.Height;

            g.InterpolationMode = InterpolationMode.NearestNeighbor;

            g.DrawImage(bmp, new Rectangle(10, 10, 120, 120),

                new Rectangle(0, 0, width, height),

                GraphicsUnit.Pixel);

            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(bmp,

                new Rectangle(130, 10, 120, 120),

                new Rectangle(0, 0, width, height),

                GraphicsUnit.Pixel);

            g.Dispose();

        }

 

        private void button10_Click(object sender, EventArgs e)

        {

            Graphics g = this.CreateGraphics();

            Bitmap bmp = new Bitmap("1.jpg");

           // g.FillRectangle(Brushes.Blue, this.ClientRectangle);

            Rectangle sr = new Rectangle(80, 60, 400, 400);

            Rectangle dr = new Rectangle(0, 0, 200, 200);

            g.DrawImage(bmp, dr, sr, GraphicsUnit.Pixel);

            g.Dispose();

        }

 

        private void button11_Click(object sender, EventArgs e)

        {

            Graphics g = this.CreateGraphics();

            Bitmap bmp = new Bitmap("1.jpg");

            Point[] destinationPoints ={

                                          new Point(0,0),

                                          new Point(100,0),

                                          new Point(50,100)

                                      };

            g.DrawImage(bmp, destinationPoints);

        }

 

        private void button12_Click(object sender, EventArgs e)

        {

            Graphics g = this.CreateGraphics();

            Bitmap bmp = new Bitmap("1.jpg");

            int width = bmp.Width;

            int height = bmp.Height;

            Point[] ps = { new Point(width, height), new Point(0, height), new Point(width, 0) };

            g.DrawImage(bmp, ps);

            g.Dispose();

        }

 

        private void button13_Click(object sender, EventArgs e)

        {

            Graphics g = this.CreateGraphics();

            Bitmap bmp = new Bitmap("1.jpg");

 

            Rectangle r = new Rectangle(0, 0, 100, 100);

            Bitmap bmp2 = bmp.Clone(r, PixelFormat.DontCare);

            g.DrawImage(bmp,new Rectangle(0,0,200,200));

            g.DrawImage(bmp2,new Rectangle(210,0,200,200));

            bmp.Dispose();

            bmp2.Dispose();

            g.Dispose();

        }

 

        public bool ThumbnailCallback()

        {

            return false;

        }

        private void button14_Click(object sender, EventArgs e)

        {

            Graphics g = this.CreateGraphics();

            Bitmap bmp = new Bitmap("1.jpg");

 

            Image.GetThumbnailImageAbort thu = new Image.GetThumbnailImageAbort(ThumbnailCallback);

            Image im = bmp.GetThumbnailImage(40, 40,thu, IntPtr.Zero);

            g.DrawImage(im, 0, 0, im.Width, im.Height);

            im.Dispose();

        }

 

        private void button15_Click(object sender, EventArgs e)

        {

            Bitmap bmp = new Bitmap(100, 100);

            Graphics gImage = Graphics.FromImage(bmp);

            gImage.FillRectangle(Brushes.Red, 0, 0, bmp.Width, bmp.Height);

            gImage.DrawRectangle(Pens.Black, 10, 10, bmp.Width - 20, bmp.Height - 20);

            Graphics gScreen = this.CreateGraphics();

            gScreen.FillRectangle(Brushes.White, this.ClientRectangle);

            gScreen.DrawImage(bmp,new Rectangle(10,10,bmp.Width,bmp.Height));

            gScreen.Dispose();

            gImage.Dispose();

        }

 

        private void button16_Click(object sender, EventArgs e)

        {

            Graphics gForm = this.CreateGraphics();

            for (int i = 1; i <= 7; ++i)

            {

                Rectangle t = new Rectangle(i * 40 - 15, 0, 15, this.ClientRectangle.Height);

                gForm.FillRectangle(Brushes.Orange,t);

            }

            Bitmap bmp = new Bitmap(260, 260, PixelFormat.Format32bppArgb);

            Graphics gBmp = Graphics.FromImage(bmp);

            gBmp.CompositingMode = CompositingMode.SourceCopy;

 

            Color red = Color.FromArgb(0x60, 0xff, 0, 0);

            Brush redBrush = new SolidBrush(red);

            gBmp.FillEllipse(redBrush, 70, 70, 160, 160);

 

            Color green = Color.FromArgb(0x40, 0, 0xff, 0);

            Brush greenBrush = new SolidBrush(green);

            gBmp.FillRectangle(greenBrush, 10, 10, 140, 140);

            gForm.DrawImage(bmp, 20, 20, bmp.Width, bmp.Height);

            bmp.Dispose();

            gBmp.Dispose();

            redBrush.Dispose();

            greenBrush.Dispose();

        }

 

        private void button17_Click(object sender, EventArgs e)

        {

            Graphics gForm = this.CreateGraphics();

            Bitmap bmp = new Bitmap(6, 6);

            Graphics gBmp = Graphics.FromImage(bmp);

            gBmp.FillRectangle(Brushes.White, 0, 0, bmp.Width, bmp.Height);

           // gBmp.InterpolationMode = InterpolationMode.HighQualityBicubic;

            gBmp.DrawLine(Pens.Red, 0, 0, 5, 5);

            gForm.DrawImage(bmp, 20, 20, bmp.Width, bmp.Height);

            for (int y = 0; y < bmp.Height; ++y)

            {

                for (int x = 0; x < bmp.Width; ++x)

                {

                    Color c = bmp.GetPixel(x, y);

                    Console.WriteLine("{0,2:x}{1,2:x}{2,2:x}{3,2:x}", c.A, c.R, c.G, c.B);

                }

                Console.WriteLine();

            }

            bmp.Dispose();

        }

 

        private void button18_Click(object sender, EventArgs e)

        {

            Graphics g = this.CreateGraphics();

            Bitmap bmp = new Bitmap("1.jpg");

            g.FillRectangle(Brushes.White, this.ClientRectangle);

            for (int i = 1; i <= 7; ++i)

            {

                Rectangle r = new Rectangle(i * 40 - 15, 0, 15, this.ClientRectangle.Height);

                g.FillRectangle(Brushes.Gray, r);

            }

            float[][] matrixItems =

            {

                new float[]{1,0,0,0,0},

                new float[]{0,1,0,0,0},

                new float[]{0,0,1,0,0},

                new float[]{0,0,0,0.2f,0},

                new float[]{0,0,0,0,1}

            };

            ColorMatrix colorMatrix = new ColorMatrix(matrixItems);

            ImageAttributes att = new ImageAttributes();

            att.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

            g.DrawImage(bmp, this.ClientRectangle, 0.0f, 0.0f, bmp.Width, bmp.Height, GraphicsUnit.Pixel, att);

            att.Dispose();

        }

说明:

button1_Click将图像简单的画到窗体上

Form1_Paint窗体重绘事件

SetStyle(ControlStyles.DoubleBuffer, true);

SetStyle(ControlStyles.AllPaintingInWmPaint, true);

SetStyle(ControlStyles.UserPaint, true);

设置图像二次缓冲

 

ContainerControl 如果为 true,则控件是类似容器的控件。

 UserPaint 如果为 true,控件将自行绘制,而不是通过操作系统来绘制。如果为 false,将不会引发 Paint 事件。此样式仅适用于派生自 Control 的类。

 Opaque 如果为 true,则控件被绘制为不透明的,不绘制背景。

 ResizeRedraw 如果为 true,则在调整控件大小时重绘控件。

 FixedWidth 如果为 true,则自动缩放时,控件具有固定宽度。例如,如果布局操作试图重新缩放控件以适应新的 Font,则控件的 Width 将保持不变。

 FixedHeight 如果为 true,则自动缩放时,控件具有固定高度。例如,如果布局操作试图重新缩放控件以适应新的 Font,则控件的 Height 将保持不变。

 StandardClick 如果为 true,则控件将实现标准 Click 行为。

 Selectable 如果为 true,则控件可以接收焦点。

 UserMouse 如果为 true,则控件完成自己的鼠标处理,因而鼠标事件不由操作系统处理。

 SupportsTransparentBackColor 如果为 true,控件接受 alpha 组件小于 255 BackColor 以模拟透明。仅在 UserPaint 位设置为 true 并且父控件派生自 Control 时才模拟透明。

 StandardDoubleClick 如果为 true,则控件将实现标准 DoubleClick 行为。如果 StandardClick 位未设置为 true,则忽略此样式。

 AllPaintingInWmPaint 如果为 true,控件将忽略 WM_ERASEBKGND 窗口消息以减少闪烁。仅当 UserPaint 位设置为 true 时,才应当应用该样式。

 CacheText 如果为 true,控件保留文本的副本,而不是在每次需要时从 Handle 获取文本副本。此样式默认为 false。此行为提高了性能,但使保持文本同步变得困难。

 EnableNotifyMessage 如果为 true,则为发送到控件的 WndProc 的每条消息调用 OnNotifyMessage 方法。此样式默认为 falseEnableNotifyMessage 在部分可信的情况下不工作。

 DoubleBuffer 如果为 true,则绘制在缓冲区中进行,完成后将结果输出到屏幕上。双重缓冲区可防止由控件重绘引起的闪烁。如果将 DoubleBuffer 设置为 true,则还应当将 UserPaint AllPaintingInWmPaint 设置为 true

 OptimizedDoubleBuffer 如果为 true,则该控件首先在缓冲区中绘制,而不是直接绘制到屏幕上,这样可以减少闪烁。如果将此属性设置为 true,则还应当将 AllPaintingInWmPaint 设置为 true

 UseTextForAccessibility 指定该控件的 Text 属性的值,如果已设置,则可确定该控件的默认 Active Accessibility 名称和快捷键。

 

button2_Click检索与已安装的图像编码器和解码器(统称编码解码器)相关的所有信息

button3_ClickPixelFormat 枚举

Indexed 该像素数据包含颜色索引值,这意味着这些值是系统颜色表中颜色的索引,而不是单个颜色值。

 Gdi 像素数据包含 GDI 颜色。

 Alpha 像素数据包含没有进行过自左乘的 alpha 值。

 PAlpha 像素格式包含自左乘的 alpha 值。

 Extended 保留。

 Canonical 默认像素格式,每像素 32 位。此格式指定 24 位颜色深度和一个 8 alpha 通道。

 Undefined 未定义像素格式。

 DontCare 没有指定像素格式。

 Format1bppIndexed 指定像素格式为每像素 1 位,并指定它使用索引颜色。因此颜色表中有两种颜色。

 Format4bppIndexed 指定格式为每像素 4 位而且已创建索引。

 Format8bppIndexed 指定格式为每像素 8 位而且已创建索引。因此颜色表中有 256 种颜色。

 Format16bppGrayScale 像素格式为每像素 16 位。该颜色信息指定 65536 种灰色调。

 Format16bppRgb555 指定格式为每像素 16 位;红色、绿色和蓝色分量各使用 5 位。剩余的 1 位未使用。

 Format16bppRgb565 指定格式为每像素 16 位;红色分量使用 5 位,绿色分量使用 6 位,蓝色分量使用 5 位。

 Format16bppArgb1555 像素格式为每像素 16 位。该颜色信息指定 32,768 种色调,其中 5 位为红色,5 位为绿色,5 位为蓝色,1 位为 alpha

 Format24bppRgb 指定格式为每像素 24 位;红色、绿色和蓝色分量各使用 8 位。

 Format32bppRgb 指定格式为每像素 32 位;红色、绿色和蓝色分量各使用 8 位。剩余的 8 位未使用。

 Format32bppArgb 指定格式为每像素 32 位;alpha、红色、绿色和蓝色分量各使用 8 位。

 Format32bppPArgb 指定格式为每像素 32 位;alpha、红色、绿色和蓝色分量各使用 8 位。根据 alpha 分量,对红色、绿色和蓝色分量进行自左乘。

 Format48bppRgb 指定格式为每像素 48 位;红色、绿色和蓝色分量各使用 16 位。

 Format64bppArgb 指定格式为每像素 64 位;alpha、红色、绿色和蓝色分量各使用 16 位。

 Format64bppPArgb 指定格式为每像素 64 位;alpha、红色、绿色和蓝色分量各使用 16 位。根据 alpha 分量,对红色、绿色和蓝色分量进行自左乘。

 Max 此枚举的最大值。

 

button4_Click获取分辨率,图像大小等基本参数

button6_ClickDrawImage参数使用练习

button7_Click改变图像分辨率

button8_Click改变图像分辨率

button9_Click图像放缩插值方法

Invalid 等效于 QualityMode 枚举的 Invalid 元素。

 Default 指定默认模式。

 Low 指定低质量插值法。

 High 指定高质量插值法。

 Bilinear 指定双线性插值法。不进行预筛选。将图像收缩为原始大小的 50% 以下时,此模式不适用。 

 Bicubic 指定双三次插值法。不进行预筛选。将图像收缩为原始大小的 25% 以下时,此模式不适用。

 NearestNeighbor 指定最临近插值法。

 HighQualityBilinear 指定高质量的双线性插值法。执行预筛选以确保高质量的收缩。 

 HighQualityBicubic 指定高质量的双三次插值法。执行预筛选以确保高质量的收缩。此模式可产生质量最高的转换图像。

button10_Click裁剪图像和绘制指定大小的裁剪图像

button11_Click变换图像

方法的一个重载方法允许把一个三点数组作为一个参数传送给它。这三个点在绘制图像时执行下述函数:

第一点指定源位图的左上角点的目标位置

第二点指定源位图的右上角点的目标位置

第三点指定源位图的左下角点的目标位置

只要为这三个目标点选择合适的值,就可以对图像进行缩放、变形、水平或垂直的翻转,以及旋转操作。

button12_Click图像翻转

button13_Click图像克隆

克隆原图像r区域的图片,然后在Rectangle(210,0,200,200)位置显示

button14_Click 缩略图

button15_Click简单绘图

button16_Click透明图

图像透明是有alpha分量来控制的

button17_Click获取图像个像素点

button18_Click颜色矩阵ColorMatrix的使用

很惭愧我还不能完全理解ColorMatrix的各种组合方式带来的预期效果

本人也在学习GDI+,写得比较简单,让高手见笑了。欢迎高手给我指点

邮箱:bobui@163.com

QQ125941562

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值