类库探源——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 }

效果

附本小节代码下载

常用静态方法:

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");
        }
    }
}

附本小节代码下载

时间: 2024-08-12 09:15:56

类库探源——System.Drawing的相关文章

类库探源——System.Drawing.Bitmap

一.System.Drawing.Bitmap Bitmap 类: 封装GDI+ 位图,此位图由图形图像及其属性的像素数据组成.Bitmap 是用于处理由像素定义的图像的对象 命名空间: System.Drawing 程序集:   System.Drawing.dll 继承关系: 原型定义: [SerializableAttribute] [ComVisibleAttribute(true)] public sealed class Bitmap : Image 备注:GDI+ 支持下列文件格式

类库探源——System.Delegate

一.MSDN 描述 Delegate 类:表示委托,委托是一种数据结构,它引用静态方法或引用类实例及该类的实例方法.(是不是感觉很像C语言中的函数指针 :) ) 命名空间: System 程序集:   mscorlib.dll 说到 Delegate 就必须谈 MulticastDelagate MulticastDelagate类 :表示多路广播委托:即,其调用列表中可以拥有多个元素的委托. 命名空间: System 程序集:   mscorlib.dll 继承关系: 备注: 1. Deleg

类库探源——System.String

一.MSDN描述 String 类: 表示文本,即一系列的 Unicode 字符 命名空间 : System 程序集 : mscorlib.dll 继承关系: 备注: 1. 字符串是 Unicode 字符的有序集合,用于表示文本.String 对象是 System.Char 对象的有序集合,用于表示字符串. 2. String 对象的值是该有序集合的内容,值不可变,所以String对象称为不可变的 string str1 = "3"+"b"; 这句话设计到1个Str

类库探源——System.Configuration 配置信息处理

按照MSDN描述 System.Configuration 命名空间 包含处理配置信息的类型 本篇文章主要两方面的内容 1. 如何使用ConfigurationManager 读取AppSetting和ConnectionStrings 2. 如何使用自定义 Section,我这里的自定义Section格式为 <SectionName> <services> 服务1的描述信息,供IoC容器使用 服务2的描述信息,供IoC容器使用 ... </services> <

类库探源——System.ValueType

一.MSDN描述 ValueType 类:提供值类型的基类 命名空间: System 程序集:   mscorlib.dll 继承关系: 值类型包括:字符.整数.浮点.布尔.枚举.结构(其实字符.整数.浮点.布尔是结构,下面会说明) 二.值类型花名册 1. 字符 Char 结构: 表示一个 Unicode 字符. 命名空间:   System 程序集   : mscorlib.dll 原型定义: [SerializableAttribute] [ComVisibleAttribute(true)

类库探源——System.Environment

Environment 类: 提供有关当前环境和平台的信息以及操作它们的方法.此类不能被继承. 命名空间: System 程序集:   mscorlib.dll 继承关系: 常用属性(字段)和方法: CurrentDirectory      获取或设置当前工作目录的完全限定路径 OSVersion             获取包含当前平台标识符和版本号的 OperatingSystem 对象. GetLogicalDrives      返回包含当前计算机中的逻辑驱动器名称的字符串数组. Ge

类库探源——System.Type

一.MSDN 描述 Type 类:表示类型声明:类类型.接口类型.数组类型.值类型.枚举类型.类型参数.泛型类型定义.以及开放或封闭构造的泛型类型. 命名空间: System 程序集:mscorlib.dll 继承关系: 从上面的继承关系能看出,Type和反射有关系,的确,引用MSDN上的话Type 为 System.Relection 功能的根也是访问元数组的主要方式. 二.获取 Type 的几种方式: 1. typeof 运算符 1 var type1 = typeof(TypeName);

类库探源——System.Math 和 Random

一.System.Math Math类:为三角函数.对数函数和其他通用数学函数提供常数和静态方法 命名空间: System 程序集 :   mscorlib.dll 继承关系: 常用属性: Math.E     表示自然对数的底(e) Math.PI    圆周率(π) 常用方法: Math.Abs(整数.浮点数)                绝对值 Math.Sin                                    正弦 Math.Cos                

让System.Drawing.Bitmap可以在linux运行

.net core的bitmap使用的是以下类库,但无法在linux运行 https://github.com/CoreCompat/CoreCompat 在linux运行需要安装runtime.linux-x64.CoreCompat.System.Drawing  如果你的是.net core2.0,那么是使用v2类库 https://github.com/CoreCompat/System.Drawing