C# 第九章『图形、图像』◆第3节:图像处理、Image类、Bitmap 类

        一、概述

        位图由图形图像及其属性的像素数据组成。 有许多标准格式用于将位图保存到文件。 GDI+支持以下文件格式:BMP、GIF、EXIF、JPG、PNG 和 TIFF。

        GDI+提供了Image、Bitmap和Metafile等类用于图像处理,为用户进行图像格式的加载、变换和保存等操作提供了方便。

        Windows GDI+提供图像类,用于处理光栅图像 (位图) 和矢量图像 (图元文件) 。 Bitmap 类和 Metafile 类都继承自 Image 类。 Bitmap 类通过提供用于加载、保存和操作光栅图像的其他方法扩展图像类的功能。 Metafile 类通过提供用于记录和检查矢量图像的其他方法扩展图像类的功能。

        1、Image类

        Image类是为Bitmap和Metafile的类提供功能的抽象基类。

        2、Metafile类

        Metafile类定义图形图元文件,图元文件包含描述一系列图形操作的记录,这些操作可以被记录(构造)和被回放(显示)。

        3、Bitmap类

        封装GDI+位图,此位图有图形图像及其属性的像素数据组成,Bitmap是用于处理由像素数据定义的图像的对象,它属于System.Drawing命名空间,该命名空间提供了对GDI+基本图形功能的访问。

        二、Image 类

        1、定义

        为源自 Bitmap 和 Metafile 的类提供功能的抽象基类。

  • 继承:Object→MarshalByRefObject→Image
  • 派生:System.Drawing.Bitmap,System.Drawing.Imaging.Metafile
  • 属性:TypeConverterAttribute,ComVisibleAttribute,SerializableAttribute
  • 实现:ICloneable,IDisposable,ISerializable
[System.ComponentModel.TypeConverter(typeof(System.Drawing.ImageConverter))]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public abstract class Image : MarshalByRefObject, ICloneable, IDisposable, System.Runtime.Serialization.ISerializable

        2、Image 类的属性

Flags  获取该 Image 的像素数据的特性标志。
FrameDimensionsList  获取 GUID 的数组,这些 GUID 表示此 Image 中帧的维数。
Height  获取此 Image 的高度(以像素为单位)。
HorizontalResolution  获取此 Image 的水平分辨率(以“像素/英寸”为单位)。
Palette  获取或设置用于此 Image 的调色板。
PhysicalDimension  获取此图像的宽度和高度。
PixelFormat  获取此 Image 的像素格式。
PropertyIdList  获取存储于该 Image 中的属性项的 ID。
PropertyItems  获取存储于该 Image 中的所有属性项(元数据片)。
RawFormat  获取此 Image 的文件格式。
Size  获取此图像的宽度和高度(以像素为单位)。
Tag  获取或设置提供有关图像的附加数据的对象。
VerticalResolution  获取此 Image 的垂直分辨率(以“像素/英寸”为单位)。
Width  获取此 Image 的宽度(以像素为单位)。

        3、Image 类的方法

Flags  获取该 Image 的像素数据的特性标志。
FrameDimensionsList  获取 GUID 的数组,这些 GUID 表示此 Image 中帧的维数。
Height  获取此 Image 的高度(以像素为单位)。
HorizontalResolution  获取此 Image 的水平分辨率(以“像素/英寸”为单位)。
Palette  获取或设置用于此 Image 的调色板。
PhysicalDimension  获取此图像的宽度和高度。
PixelFormat  获取此 Image 的像素格式。
PropertyIdList  获取存储于该 Image 中的属性项的 ID。
PropertyItems  获取存储于该 Image 中的所有属性项(元数据片)。
RawFormat  获取此 Image 的文件格式。
Size  获取此图像的宽度和高度(以像素为单位)。
Tag  获取或设置提供有关图像的附加数据的对象。
VerticalResolution  获取此 Image 的垂直分辨率(以“像素/英寸”为单位)。
Width  获取此 Image 的宽度(以像素为单位)。
Clone()  创建此 Image 的一个精确副本。
CreateObjRef(Type)  创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。(继承自 MarshalByRefObject)
Dispose()  释放此 Image 使用的所有资源。
Dispose(Boolean)  释放由 Image 占用的非托管资源,还可以另外再释放托管资源。
Equals(Object)  确定指定对象是否等于当前对象。(继承自 Object)
Finalize()  在垃圾回收将某一对象回收前允许该对象尝试释放资源并执行其他清理操作。
FromFile(String)  从指定的文件创建 Image。
FromFile(String, Boolean)  使用该文件中的嵌入颜色管理信息,从指定的文件创建 Image。
FromHbitmap(IntPtr)  从 GDI 位图的句柄创建 Bitmap。
FromHbitmap(IntPtr, IntPtr)  从 GDI 位图的句柄和 GDI 调色板的句柄创建 Bitmap。
FromStream(Stream)  从指定的数据流创建 Image。
FromStream(Stream, Boolean)  可以选择使用指定的数据流中嵌入的颜色管理信息,从该数据流创建 Image。
FromStream(Stream, Boolean, Boolean)  可以选择使用嵌入的颜色管理信息并验证图像数据来从指定的数据流创建 Image。
GetBounds(GraphicsUnit)  以指定的单位获取图像的界限。
GetEncoderParameterList(Guid)  返回有关指定的图像编码器所支持的参数的信息。
GetFrameCount(FrameDimension)  返回指定维度的帧数。
GetHashCode()  作为默认哈希函数。(继承自 Object)
GetLifetimeService()  检索控制此实例的生存期策略的当前生存期服务对象。(继承自 MarshalByRefObject)
GetPixelFormatSize(PixelFormat)  返回指定像素格式的颜色深度(每个像素的位数)。
GetPropertyItem(Int32)  从该 Image 获取指定的属性项。
GetThumbnailImage(Int32, Int32, Image+GetThumbnailImageAbort, IntPtr)  返回此 Image 的缩略图。
GetType()  获取当前实例的 Type。(继承自 Object)
InitializeLifetimeService()  获取生存期服务对象来控制此实例的生存期策略。(继承自 MarshalByRefObject)
IsAlphaPixelFormat(PixelFormat)  返回一个值,该值指示此 Image 的像素格式是否包含 alpha 信息。
IsCanonicalPixelFormat(PixelFormat)  返回一个值,该值指示该像素格式是否为每个像素 32 位。
IsExtendedPixelFormat(PixelFormat)  返回一个值,该值指示该像素格式是否为每个像素 64 位。
MemberwiseClone()  创建当前 Object 的浅表副本。(继承自 Object)
MemberwiseClone(Boolean)  创建当前 MarshalByRefObject 对象的浅表副本。(继承自 MarshalByRefObject)
RemovePropertyItem(Int32)  从该 Image 移除指定的属性项。
RotateFlip(RotateFlipType)  旋转、翻转或者同时旋转和翻转 Image。
Save(Stream, ImageCodecInfo, EncoderParameters)  使用指定的编码器和图像编码器参数,将该图像保存到指定的流。
Save(Stream, ImageFormat)  将此图像以指定的格式保存到指定的流中。
Save(String)  将该 Image 保存到指定的文件或流。
Save(String, ImageCodecInfo, EncoderParameters)  使用指定的编码器和图像编码器参数,将该 Image 保存到指定的文件。
Save(String, ImageFormat)  将此 Image 以指定格式保存到指定文件。
SaveAdd(EncoderParameters)  将帧添加到上次对 Save 方法调用时所指定的文件或流中。 使用此方法将多帧图像中的选定帧保存到另一个多帧图像。
SaveAdd(Image, EncoderParameters)  将帧添加到上次对 Save 方法调用时所指定的文件或流中。
SelectActiveFrame(FrameDimension, Int32)  选择由维度和索引指定的帧。
SetPropertyItem(PropertyItem)  在此 Image 中存储一个属性项(元数据片)。
ToString()  返回表示当前对象的字符串。(继承自 Object)

        4、Image 类的显式接口实现

ISerializable.GetObjectData(SerializationInfo, StreamingContext)  使用将目标对象序列化所需的数据填充 SerializationInfo。

        5、示例

下面的代码示例设计用于Windows 窗体。 它是事件的 Paint 处理程序。 对象 Graphics 将传递给事件,用于在窗体上绘制图像。 此代码执行以下操作:

    从名为 SampImag.jpg 的文件创建映像。 此文件必须与应用程序可执行文件位于同一文件夹中。

    创建绘制图像左上角的点。

    绘制窗体上的未缩放图像。

private void ImageExampleForm_Paint(object sender, PaintEventArgs e)
{         
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create Point for upper-left corner of image.
    Point ulCorner = new Point(100, 100);
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, ulCorner);
}

        三、Bitmap 类

Bitmap 类https://learn.microsoft.com/zh-cn/dotnet/api/system.drawing.bitmap?view=netframework-4.0

        在使用窗体或控件的CreateGraphics方法创建的Graphics绘图对象,这导致绘制的图像都是暂时的,如果当前窗体被切换或被其他窗口覆盖,这些图像就会消失,为了使图像一直显示,可以通过在窗体或控件的Bitmap对象上绘制图像来实现。

        1、定义

        封装 GDI+ 位图,此位图由图形图像及其属性的像素数据组成。 Bitmap 是用于处理由像素数据定义的图像的对象。

        可以使用其中一 Bitmap 个构造函数从文件、流和其他源创建图像,并使用该方法将其保存到流或文件系统 Save 。 图像通过使用 DrawImage 对象的方法 Graphics 绘制到屏幕或内存中。 有关处理图像文件的主题列表,请参阅 使用图像、位图、图标和图元文件。

  • 命名空间:System.Drawing
  • 程序集:System.Drawing.dll
  • 继承:Object→MarshalByRefObject→Image→Bitmap
  • 属性:ComVisibleAttribute,SerializableAttribute
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public sealed class Bitmap : System.Drawing.Image

        2、Bitmap 类的构造函数

Bitmap(Image)  从指定的现有图像初始化 Bitmap 类的新实例。
Bitmap(Image, Int32, Int32)  从指定的现有图像(缩放到指定大小)初始化 Bitmap 类的新实例。
Bitmap(Image, Size)  从指定的现有图像(缩放到指定大小)初始化 Bitmap 类的新实例。
Bitmap(Int32, Int32)  用指定的大小初始化 Bitmap 类的新实例。
Bitmap(Int32, Int32, Graphics)  用指定的大小和指定的 Graphics 对象的分辨率初始化 Bitmap 类的新实例。
Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr)  用指定的大小、像素格式和像素数据初始化 Bitmap 类的新实例。
Bitmap(Int32, Int32, PixelFormat)  用指定的大小和格式初始化 Bitmap 类的新实例。
Bitmap(Stream)  从指定的数据流初始化 Bitmap 类的新实例。
Bitmap(Stream, Boolean)  从指定的数据流初始化 Bitmap 类的新实例。
Bitmap(String)  从指定的文件初始化 Bitmap 类的新实例。
Bitmap(String, Boolean)  从指定的文件初始化 Bitmap 类的新实例。
Bitmap(Type, String)  从指定的资源初始化 Bitmap 类的新实例。

        3、Bitmap 类的属性

Flags  获取该 Image 的像素数据的特性标志。(继承自 Image)
FrameDimensionsList  获取 GUID 的数组,这些 GUID 表示此 Image 中帧的维数。(继承自 Image)
Height  获取此 Image 的高度(以像素为单位)。(继承自 Image)
HorizontalResolution  获取此 Image 的水平分辨率(以“像素/英寸”为单位)。(继承自 Image)
Palette  获取或设置用于此 Image 的调色板。(继承自 Image)
PhysicalDimension  获取此图像的宽度和高度。(继承自 Image)
PixelFormat  获取此 Image 的像素格式。(继承自 Image)
PropertyIdList  获取存储于该 Image 中的属性项的 ID。(继承自 Image)
PropertyItems  获取存储于该 Image 中的所有属性项(元数据片)。(继承自 Image)
RawFormat  获取此 Image 的文件格式。(继承自 Image)
Size  获取此图像的宽度和高度(以像素为单位)。(继承自 Image)
Tag  获取或设置提供有关图像的附加数据的对象。(继承自 Image)
VerticalResolution  获取此 Image 的垂直分辨率(以“像素/英寸”为单位)。(继承自 Image)
Width  获取此 Image 的宽度(以像素为单位)。(继承自 Image)

        4、Bitmap 类的方法

Clone()  创建此 Image 的一个精确副本。(继承自 Image)
Clone(Rectangle, PixelFormat)  创建此 Bitmap(它由 Rectangle 结构并使用指定的 PixelFormat 枚举定义)部分的副本。
Clone(RectangleF, PixelFormat)  创建此 Bitmap(用指定的 PixelFormat 枚举定义)部分的副本。
CreateObjRef(Type)  创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。(继承自 MarshalByRefObject)
Dispose()  释放此 Image 使用的所有资源。(继承自 Image)
Dispose(Boolean)  释放由 Image 占用的非托管资源,还可以另外再释放托管资源。(继承自 Image)
Equals(Object)  确定指定对象是否等于当前对象。(继承自 Object)
FromHicon(IntPtr)  从图标的 Windows 图柄创建 Bitmap。
FromResource(IntPtr, String)  从指定的 Windows 资源创建 Bitmap。
GetBounds(GraphicsUnit)  以指定的单位获取图像的界限。(继承自 Image)
GetEncoderParameterList(Guid)  返回有关指定的图像编码器所支持的参数的信息。(继承自 Image)
GetFrameCount(FrameDimension)  返回指定维度的帧数。(继承自 Image)
GetHashCode()  作为默认哈希函数。(继承自 Object)
GetHbitmap()  从此 Bitmap创建 GDI 位图对象。
GetHbitmap(Color)  从此 Bitmap创建 GDI 位图对象。
GetHicon()  返回图标的图柄。
GetLifetimeService()  检索控制此实例的生存期策略的当前生存期服务对象。(继承自 MarshalByRefObject)
GetPixel(Int32, Int32)  获取此 Bitmap 中指定像素的颜色。
GetPropertyItem(Int32)  从该 Image 获取指定的属性项。(继承自 Image)
GetThumbnailImage(Int32, Int32, Image+GetThumbnailImageAbort, IntPtr)  返回此 Image 的缩略图。(继承自 Image)
GetType()  获取当前实例的 Type。(继承自 Object)
InitializeLifetimeService()  获取生存期服务对象来控制此实例的生存期策略。(继承自 MarshalByRefObject)
LockBits(Rectangle, ImageLockMode, PixelFormat)  将 Bitmap 锁定到系统内存中。
LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData)  将 Bitmap 锁定到系统内存中。
MakeTransparent()  让 Bitmap 的默认透明颜色为透明。
MakeTransparent(Color)  使指定的颜色对此 Bitmap 透明。
MemberwiseClone()  创建当前 Object 的浅表副本。(继承自 Object)
MemberwiseClone(Boolean)  创建当前 MarshalByRefObject 对象的浅表副本。(继承自 MarshalByRefObject)
RemovePropertyItem(Int32)  从该 Image 移除指定的属性项。(继承自 Image)
RotateFlip(RotateFlipType)  旋转、翻转或者同时旋转和翻转 Image。(继承自 Image)
Save(Stream, ImageCodecInfo, EncoderParameters)  使用指定的编码器和图像编码器参数,将该图像保存到指定的流。(继承自 Image)
Save(Stream, ImageFormat)  将此图像以指定的格式保存到指定的流中。(继承自 Image)
Save(String)  将该 Image 保存到指定的文件或流。(继承自 Image)
Save(String, ImageCodecInfo, EncoderParameters)  使用指定的编码器和图像编码器参数,将该 Image 保存到指定的文件。(继承自 Image)
Save(String, ImageFormat)  将此 Image 以指定格式保存到指定文件。(继承自 Image)
SaveAdd(EncoderParameters)  将帧添加到上次对 Save 方法调用时所指定的文件或流中。 使用此方法将多帧图像中的选定帧保存到另一个多帧图像。(继承自 Image)
SaveAdd(Image, EncoderParameters)  将帧添加到上次对 Save 方法调用时所指定的文件或流中。(继承自 Image)
SelectActiveFrame(FrameDimension, Int32)  选择由维度和索引指定的帧。(继承自 Image)
SetPixel(Int32, Int32, Color)  在此 Bitmap 中设置指定像素的颜色。
SetPropertyItem(PropertyItem)  在此 Image 中存储一个属性项(元数据片)。(继承自 Image)
SetResolution(Single, Single)  设置此 Bitmap 的分辨率。
ToString()  返回表示当前对象的字符串。(继承自 Object)
UnlockBits(BitmapData)  从系统内存解锁此 Bitmap。

        5、Bitmap 类的显式接口实现

ISerializable.GetObjectData(SerializationInfo, StreamingContext)  使用将目标对象序列化所需的数据填充 SerializationInfo。(继承自 Image)

        6、示例

        从指定的现有图像建立Bitmap对象

Bitmap box1 =new Bitmap(pictureBox1.Image);

        从指定的图像文件建立Bitmap对象,其中001.png是已经存在的图像文件

Bitmap box2 =new Bitmap("D:\\001.png");

        从现有的Bitmap对象建立新的Bitmap对象

Bitmap box3 = new Bitmap(box1);

        7、刷新图像

        使用Bitmap对象绘制图像时,可以先创建一个Bitmap对象,并在其上绘制图像,再将其赋值给窗体或控件的Bitmap对象,这样绘制出的图像就可以自动刷新,不再使用程序来绘制图像。

        ①创建Bitmap对象时,需要使用Bitmap类的构造函数,代码如下:

创建指定大小的Bitmap对象
Bitmap Image1 = new Bitmap(120,80);

        ②创建完Bitmap对象之后,使用创建的Bitmap对象生成Graphics绘制对象,然后调用Graphics绘图对象的相关方法绘制图像,代码如下:

创建Graphics对象
Graphics gg1 = Graphics.FromImage(Image1);

Pen pp1 = new Pen(Color.Green,3);

gg1.DrawEllipse(pp1,50,10,120,80);

        ③将Bitmap对象指定给窗体或控件的Bitmap对象。

this.BackroundImage = Image1;

        这样通过以上步骤绘制出的图像就会自动刷新,并一直显示。

        四、图像的输入与保存

        1、图像的输入

        在窗体或图形框内输入图像有以下两种方式:

        方法一:在窗体上,建立一个pictureBox,在其属性中的Image属性里选择图像文件

        方法二:在窗体上,建立一个pictureBox,而后在代码中写入

using System;
using System.Drawing;
using System.Windows.Forms;

namespace _20220921_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            String imageFileName = ("D:/Documents/Desktop/2001.png");
            pictureBox1.Image = new Bitmap(imageFileName);
        }
    }
}

        2、图像的保存

using System;
using System.Drawing;
using System.Windows.Forms;

namespace _20220921_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd1 = new OpenFileDialog();
            ofd1.Filter = "BMP|*.png";
            if(ofd1.ShowDialog() == DialogResult.OK)
            {
                Bitmap image1 = new Bitmap(ofd1.FileName);
                pictureBox1.Image = image1;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null)
                MessageBox.Show("没有图像可保存");
            else
            {
                Bitmap box1 = new Bitmap(pictureBox1.Image);
                SaveFileDialog sfd1 = new SaveFileDialog();
                sfd1.Filter = "png格式|*.png";

                if (sfd1.ShowDialog() == DialogResult.OK)
                    box1.Save(sfd1.FileName);
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null)
                MessageBox.Show("没有图像可保存");
            else
            {
                Bitmap box1 = new Bitmap(pictureBox1.Image);
                SaveFileDialog sfd1 = new SaveFileDialog();
                sfd1.Filter = "jpeg格式|*.jpeg|bmp格式|*.BMP";

                if (sfd1.ShowDialog() == DialogResult.OK)
                    box1.Save(sfd1.FileName,System.Drawing.Imaging.ImageFormat.Jpeg);
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_45336030/article/details/126970380
今日推荐