利用System.Drawing.Image類進行圖片相關操作,
C#中對圖片的操作主要是通過System.Drawing.Image等類進行。
一、將圖片轉換為字節流
/// <summary>
/// 圖片處理幫助類
/// </summary>
public static class PicProcessHelper
{
/// <summary>
/// 將圖片轉換為指定的字節流
/// </summary>
/// <param name="filePath">圖片路徑</param>
/// <returns>指定的字節流</returns>
public static byte[] ConvertToByte(String filePath)
{
var m = new System.IO.MemoryStream();
var bp = new System.Drawing.Bitmap(filePath);
bp.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg); //將此圖像以指定的格式保存到指定的流中。
byte[] imgByte = m.GetBuffer(); //從內存緩沖區中讀取
return imgByte;
}
}
二、將字節流轉換回圖片
/// <summary>
/// 根據字節流返回Image類型
/// </summary>
/// <param name="streamByte"></param>
/// <returns></returns>
public static Image ReturnImage(byte[] streamByte)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte);
Image img = Image.FromStream(ms);
return img;
}
三、將Image對象轉換為字節流
//將Image轉換成流數據,並保存為byte[]
public static byte[] PhotoImageInsert(System.Drawing.Image imgPhoto)
{
MemoryStream mstream = new MemoryStream();
imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] byData = new Byte[mstream.Length];
mstream.Position = 0;
mstream.Read(byData, 0, byData.Length); mstream.Close();
return byData;
}
四、保存圖片
var oldFilename = @"E:\環境部署\圖片集\熊貓1.jpg";
var oldImage = System.Drawing.Image.FromFile(oldFilename);
var newFilename = @"E:\我的新熊貓.jpg";
oldImage.Save(newFilename, ImageFormat.Jpeg);
五、生成縮略圖
1 /// <summary>
2 /// 生成圖片縮略文件
3 /// </summary>
4 /// <param name="originalImage">圖片源文件</param>
5 /// <param name="width">縮略圖寬度</param>
6 /// <param name="height">縮略圖高度</param>
7 /// <param name="mode">生成縮略圖的方式</param>
8 /// <returns>縮率處理後圖片文件</returns>
9 public static Image MakeThumbnail(Image originalImage, int width, int height, ThumbnailModel mode)
10 {
11 int towidth = width;
12 int toheight = height;
13
14 int x = 0;
15 int y = 0;
16 int ow = originalImage.Width;
17 int oh = originalImage.Height;
18
19 switch (mode)
20 {
21 case ThumbnailModel.HighWidth: //指定高寬縮放(可能變形)
22 break;
23 case ThumbnailModel.Width: //指定寬,高按比例
24 toheight = originalImage.Height * width / originalImage.Width;
25 break;
26 case ThumbnailModel.Hight: //指定高,寬按比例
27 towidth = originalImage.Width * height / originalImage.Height;
28 break;
29 case ThumbnailModel.Default: //指定高,寬按比例
30 if (ow <= towidth && oh <= toheight)
31 {
32 x = -(towidth - ow) / 2;
33 y = -(toheight - oh) / 2;
34 ow = towidth;
35 oh = toheight;
36 }
37 else
38 {
39 if (ow > oh)//寬大於高
40 {
41 x = 0;
42 y = -(ow - oh) / 2;
43 oh = ow;
44 }
45 else//高大於寬
46 {
47 y = 0;
48 x = -(oh - ow) / 2;
49 ow = oh;
50 }
51 }
52 break;
53 case ThumbnailModel.Auto:
54 if (originalImage.Width / originalImage.Height >= width / height)
55 {
56 if (originalImage.Width > width)
57 {
58 towidth = width;
59 toheight = (originalImage.Height * width) / originalImage.Width;
60 }
61 else
62 {
63 towidth = originalImage.Width;
64 toheight = originalImage.Height;
65 }
66 }
67 else
68 {
69 if (originalImage.Height > height)
70 {
71 toheight = height;
72 towidth = (originalImage.Width * height) / originalImage.Height;
73 }
74 else
75 {
76 towidth = originalImage.Width;
77 toheight = originalImage.Height;
78 }
79 }
80 break;
81 case ThumbnailModel.Cut: //指定高寬裁減(不變形)
82 if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
83 {
84 oh = originalImage.Height;
85 ow = originalImage.Height * towidth / toheight;
86 y = 0;
87 x = (originalImage.Width - ow) / 2;
88 }
89 else
90 {
91 ow = originalImage.Width;
92 oh = originalImage.Width * height / towidth;
93 x = 0;
94 y = (originalImage.Height - oh) / 2;
95 }
96 break;
97 default:
98
99 break;
100 }
101
102 //新建一個bmp圖片
103 System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
104
105 //新建一個畫板
106 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
107
108 //設置高質量插值法
109 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
110
111 //設置高質量,低速度呈現平滑程度
112 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
113
114 //清空畫布並以透明背景色填充
115 g.Clear(System.Drawing.Color.White);
116
117 //在指定位置並且按指定大小繪制原圖片的指定部分
118 g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
119 new System.Drawing.Rectangle(x, y, ow, oh),
120 System.Drawing.GraphicsUnit.Pixel);
121
122 return bitmap;
123 }
View Code
其中縮略圖模式定義如下:
/// <summary>
/// 縮率圖處理模式
/// </summary>
public enum ThumbnailModel
{
/// <summary>
/// 指定高寬縮放(可能變形)
/// </summary>
HighWidth,
/// <summary>
/// 指定寬,高按比例
/// </summary>
Width,
/// <summary>
/// 默認 全圖不變形
/// </summary>
Default,
/// <summary>
/// 指定高,寬按比例
/// </summary>
Hight,
/// <summary>
/// 指定高寬裁減(不變形)??指定裁剪區域
/// </summary>
Cut,
/// <summary>
/// 自動 原始圖片按比例縮放
/// </summary>
Auto
}
View Code