类库探源——System.Drawing

一、System.Drawing 命名空间简述

System.Drawing 命名空间提供访问 GDI+ 的基本功能,更高级的功能在 System.Drawing.Drawing2D,System.Drawing.Imaging 和 System.Drawing.Text 命名空间

程序集: System.Drawing.dll

二、System.Drawing.Image 简述

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

命名空间: System.Drawing

程序集:   System.Drawing.dll

原型定义:

[SerializabaleAttribute]
[ComVisibleAttribute(true)]
[TypeConverterAttribute(typeof(ImageConverter))]
public abstract class Image : MarshalByRefObject, ISerializable, ICloneable, IDisposable

常用实例属性:

Height            获取当前图像实例的 高度(以像素为单位)

Width            获取当前图像实例的 宽度(以像素为单位)

HorizontalResolution          获取当前图像实例的水平分辨率(像素/英寸)

VerticalResolution             获取当前图像实例的垂直分辨率(像素/英寸)

PhysicalDimension           获取当前图像的宽度和高度。

RawFormat                    获取当前图像格式

PixelFormat                  获取当前Image的像素格式

代码:

 1 using System;
 2 using System.Drawing;
 3 
 4 class App
 5 {
 6     static void Main()
 7     {
 8         var img = Image.FromFile(@"图像格式.jpg");    // 图像格式.png 改扩展名而来        
 9         Console.WriteLine(img.Height);
10         Console.WriteLine(img.Width);
11         Console.WriteLine(img.HorizontalResolution);
12         Console.WriteLine(img.VerticalResolution);
13         Console.WriteLine(img.PhysicalDimension);
14         Console.WriteLine(img.PhysicalDimension.Width);
15         Console.WriteLine(img.PhysicalDimension.Height);
16         Console.WriteLine(img.RawFormat);        
17         Console.WriteLine(img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png)); // 是否是 Png 格式
18         Console.WriteLine(img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg)); // 是否是 jpg 格式 ,不是扩展名氏 jpg 不等于图像格式就是 Jpeg    
19         Console.WriteLine(img.PixelFormat);
20     }
21 }
View Code

效果

附本小节代码下载


常用静态方法:

public static Image FromFile(string filename)                  从指定文件创建 Image

public static Image FromStream(Stream stream)            从指定数据流创建 Image

常用实例方法:

public Image GetThumbnailImage(int thumbWidth,int thumbHeight,System.Drawing.Image.GetThumbnailImageAbort callback,IntPtr callbackData)       返回此 Image 的缩略图

RotateFlip                                     旋转

public void Save(string filename)       将该 Image 保存到指定的文件或流

代码:

using System;
using System.Drawing;

class App
{
    static void Main()
    {
        using(var img = Image.FromFile(@"图像格式.jpg"))
        {                
            // 生成缩略图
            var thumbImg = img.GetThumbnailImage(80,100,()=>{return false;},IntPtr.Zero);
            thumbImg.Save(@"图像格式_thumb.jpg");
            
            // 图像翻转
            var newImg = img.Clone() as Image;
            newImg.RotateFlip(RotateFlipType.Rotate180FlipX);
            newImg.Save(@"图像格式_X轴翻转180度.jpg");            
        }
    }
}

附本小节代码下载

原文地址:https://www.cnblogs.com/Aphasia/p/4158272.html