zoukankan      html  css  js  c++  java
  • 图像(一)

    GDI+提供了两个类可以用于表示图像:Bitmap类和Metafile类,这两个类都继承自Image类。Bitmap类提供了处理位图的功能,位图是基于光栅的图像,这种图像是由放在一个二维网格上的方块像素组成的,网格中的每个像素都有自己的颜色。Metafile类提供了处理矢量图像的功能,在绘图表面上绘制Metafile对象时,它会重放绘图操作,把它们一次应用到绘图表面上。

    下面的例子创建一个bitmap对象,使用它从文件中把一个位图加载到内存中,接着使用graphics类把内存中的图像显示到绘图表面上: Graphics g = e.Graphics;             Bitmap bmp = new Bitmap("a.jpg");             g.DrawImage(bmp, 0, 0);

    有时图像的颜色信息以颜色地图的形式存储(颜色表或调色板),此时位图并没有为每个像素存储红、绿、蓝颜色值,而是给每个像素存储一个整数--这个整数就是颜色地图的索引(因此表示一个颜色)。JPEG文件不适用颜色地图,但GIF文件可以使用,颜色地图最适合于简单的图像,例如图标和其他线图,但不使用大量的颜色。

    使用ImageCodecInfo类可以确定自己系统上可以使用的codecs(编解码器):

    ImageCodecInfo[] availableCodecs;             availableCodecs = ImageCodecInfo.GetImageEncoders();             int numCodecs = availableCodecs.Length;             for (int i = 0; i < numCodecs; i++)             {                 Console.WriteLine("Codec Name = " + availableCodecs[i].CodecName);                 Console.WriteLine("Class ID = " + availableCodecs[i].Clsid.ToString());                 Console.WriteLine("Filename Extension = " + availableCodecs[i].FilenameExtension);                 Console.WriteLine("Flags = " + availableCodecs[i].Flags.ToString());                 Console.WriteLine("Format Description = " + availableCodecs[i].FormatDescription);                 Console.WriteLine("Format ID = " + availableCodecs[i].FormatID.ToString());                 Console.WriteLine("MimeType = " + availableCodecs[i].MimeType);                 Console.WriteLine("Version = " + availableCodecs[i].Version.ToString());                 Console.WriteLine();             }

    像素格式是图像的一个组成部分,可以使用PixelFormat属性获得。PixelFormat属性包含的值属于PixelFormat枚举,PixelFormat枚举包含3个不同类型的成员。

    一:14个像素格式成员

    二。图像的功能

    要测试图像,看他是否有特殊功能,可以测试下面的PixelFormat枚举成员

    举例:创建一个新图像,有alpha成分,对这个图像执行上述过程的测试:

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

    bool b = ((bmp.PixelFormat & PixelFormat.Alpha) != 0);             Console.WriteLine("bmp has alpha?" + b);             bmp.Dispose();

    三。最后一个pixelformat枚举的成员允许指定在创建图像时不考虑像素格式(DontCare枚举:指定不考虑图像的像素格式)

    下面的代码显示屏幕分辨率和图像分辨率,Bitmap对象中包含在内存中的图像的高度和宽度,然后使用Size对象计算显示的宽度和屏幕上图像的高度:

    Graphics g = e.Graphics;             Bitmap bmp = new Bitmap("a.jpg");             g.DrawImage(bmp, 0, 0);             Console.WriteLine("Screen resolution:" + g.DpiX + "DPI");             Console.WriteLine("Image resolution:" + bmp.HorizontalResolution + "DPI");             Console.WriteLine("Image Width:" + 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);

    显示没有缩放的图像:

    Graphics g = e.Graphics;             Bitmap bmp = new Bitmap("a.jpg");             Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height);             g.DrawImage(bmp, r, r, GraphicsUnit.Pixel);

    缩放图像,使之正好放在客户矩形区域中

    Graphics g = e.Graphics;             Bitmap bmp = new Bitmap("a.jpg");             Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height);             g.DrawImage(bmp, this.ClientRectangle);

    调用bitmap对象的setresolution方法,改变位图的分辨率,在改变其分辨率时,会影响GDI+在绘制图像时重新设置位图大小的方式,如下代码:

    Graphics g = e.Graphics;             Bitmap bmp = new Bitmap("a.jpg");             g.FillRectangle(Brushes.White, this.ClientRectangle);             bmp.SetResolution(200f, 200f);             g.DrawImage(bmp, 0, 0);             bmp.SetResolution(100f, 100f);             g.DrawImage(bmp, 189, 0);

    设源图像的分辨率是300DPI,大小是1000*1100像素,则在内存中图像的宽度和高度是width = 1000/300 = 3.333inches , height = 1100/300 = 3.667inches,这里的要点是GDI+考虑了位图的分辨率,如果不进行显示的缩放,GDI+就会使用位图的分辨率来缩放图像。

    在GDI+需要重新设置图像的大小,以生成一个更大的图像时,它会使用一个图像放大算法来确定新像素的颜色,这个过程称为插补。差不过程使用两个点的已知值来估算或计算这两个点之间的其他点的值。GDI+允许通过赋予Graphics.InterpolationMode属性的值,编程控制图像重置大小的质量,这个属性的值是枚举类型,分别用5个值来表示质量的等级,按升序排列分别为:NearestNeighbor、Bilinear、HighQualityBilinear、Bicubie、HighQualityBicubic。

    Graphics g = e.Graphics;             Bitmap bmp = new Bitmap("a.jpg");             g.FillRectangle(Brushes.White, 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);

  • 相关阅读:
    (转)classload和class.forname()区别
    (转)HashMap和HashTable源码
    (转)spring 框架介绍
    [Spring入门学习笔记][创建网站URL]
    [spring入门学习笔记][spring的IoC原理]
    [J2EE学习][post,get乱码处理]
    [J2EE框架][Debug]
    [SQL学习笔记][用exists代替全称量词 ]
    [Spring入门学习笔记][Spring Boot]
    [Spring入门学习笔记][maven]
  • 原文地址:https://www.cnblogs.com/ttssrs/p/2396502.html
Copyright © 2011-2022 走看看