本文實(shí)例講述了C#實(shí)現(xiàn)的上傳圖片、保存圖片、加水印、生成縮略圖功能。分享給大家供大家參考,具體如下:
伴隨移動(dòng)設(shè)備地普及,處理圖片、視頻等需求也變得越來(lái)越基礎(chǔ),這里介紹的是圖片的存儲(chǔ)。
上傳圖片必須使用form表單提交的方式,我只知道這一種方法,如果大家知道其他方法的話請(qǐng)留言。
保存圖片、加水印和生成縮略圖這三種功能最好各自放在單獨(dú)的方法中,盡量降低耦合度,提高代碼復(fù)用程度,除此之外我們平常寫代碼是也要盡量做到方法功能的唯一性。
前臺(tái)代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
< form method = "POST" enctype = "multipart/form-data" action = "UploadImg.ashx" > < table > < tr > < td >func:</ td > < td >< input type = "text" name = "func" /></ td > </ tr > < tr > < td >用戶Id:</ td > < td >< input type = "text" name = "userId" /></ td > </ tr > < tr > < td >頭像:</ td > < td >< input type = "file" name = "icon" /></ td > </ tr > < tr > < td >水印:</ td > < td >< input type = "text" name = "waterMark" /></ td > </ tr > </ table > < input type = "submit" value = "提交" /> </ form > |
后臺(tái)代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
private string UploadImage(HttpContext context) { try { System.IO.Stream stream = context.Request.Files[ "icon" ].InputStream; //返回的圖片路徑可以存儲(chǔ)在數(shù)據(jù)庫(kù)中 string imageUrl = SaveImage(stream, "Icon" , "蟈蟈" ); string thumbnailImageUrl = SaveThumbnailImage(stream, "Icon" ); string thumbnailImageUrlWithWatermark = SaveThumbnailImage(ConfigurationManager.AppSettings[ "AttachmentsDirectory" ] + imageUrl, "Icon" ); return "上傳成功!" ; } catch (Exception ex) { return "上傳失敗!" ; } } private string SaveImage(Stream stream, string folderName, string waterMark) { try { string fileName = Guid.NewGuid() + ".jpg" ; string path = ConfigurationManager.AppSettings[ "AttachmentsDirectory" ]; path = Path.Combine(path, folderName + "\\" + DateTime.Now.Year + "\\" + DateTime.Now.Month + "\\" + DateTime.Now.Day + "\\" ); string imageUrl = "/" + folderName + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/" ; if (! string .IsNullOrEmpty(waterMark)) { Image imgSource = Image.FromStream(stream); AddWatermarkAndSave(path, fileName, waterMark, imgSource, imgSource.Height - 300, 10, Color.Red, new Font( "宋體" , 40)); } else { byte [] buffer = new byte [stream.Length]; stream.Read(buffer, 0, buffer.Length); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } System.IO.FileStream fs = new System.IO.FileStream(path + fileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write); fs.Write(buffer, 0, buffer.Length); fs.Flush(); fs.Close(); } return imageUrl + fileName; } catch (Exception ex) { return "" ; } } private string SaveThumbnailImage(Stream stream, string folderName) { try { string fileName = Guid.NewGuid() + ".jpg" ; string path = ConfigurationManager.AppSettings[ "AttachmentsDirectory" ]; path = Path.Combine(path, folderName + "\\" + DateTime.Now.Year + "\\" + DateTime.Now.Month + "\\" + DateTime.Now.Day + "\\" ); string imageUrl = "/" + folderName + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/" ; System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(GetFalse); //數(shù)據(jù)源來(lái)自Stream Image image = System.Drawing.Bitmap.FromStream(stream); System.Drawing.Image thumbnailImage = image.GetThumbnailImage(64, 64, myCallback, IntPtr.Zero); thumbnailImage.Save(path + fileName); thumbnailImage.Dispose(); return imageUrl + fileName; } catch (Exception ex) { return "" ; } } private string SaveThumbnailImage( string originalFileName, string folderName) { try { string fileName = Guid.NewGuid() + ".jpg" ; string path = ConfigurationManager.AppSettings[ "AttachmentsDirectory" ]; path = Path.Combine(path, folderName + "\\" + DateTime.Now.Year + "\\" + DateTime.Now.Month + "\\" + DateTime.Now.Day + "\\" ); string imageUrl = "/" + folderName + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/" ; System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(GetFalse); //數(shù)據(jù)源來(lái)自File Image image = System.Drawing.Bitmap.FromFile(originalFileName); System.Drawing.Image thumbnailImage = image.GetThumbnailImage(64, 64, myCallback, IntPtr.Zero); thumbnailImage.Save(path + fileName); thumbnailImage.Dispose(); return imageUrl + fileName; } catch (Exception ex) { return "" ; } } private bool GetFalse() { return false ; } /// <summary> /// 圖片加文字水印 /// </summary> /// <param name="fileName"> </param> /// <param name="text">水印文字,如果是多行用分號(hào)隔開(kāi)</param> /// <param name="img">圖片</param> /// <param name="paddingTop">上邊距</param> /// <param name="paddingLeft">左邊距</param> /// <param name="textColor">文字顏色</param> /// <param name="textFont">字體</param> /// <param name="path">保存地址</param> /// <returns></returns> private bool AddWatermarkAndSave( string path, string fileName, string text, Image img, int paddingTop, int paddingLeft, Color textColor, Font textFont) { text = text + ";" + "當(dāng)前時(shí)間:" + DateTime.Now.ToString( "yyyy-MM-dd HH:mm" ); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } textFont = new Font( "宋體" , 19); Bitmap bm = new Bitmap(img); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bm); System.Drawing.Brush b = new SolidBrush(textColor); string [] str = text.Split( ';' ); for ( int i = 0; i < str.Length; i++) g.DrawString(str[i], textFont, b, paddingLeft, paddingTop + 33 * i); g.Dispose(); bm.Save(path + fileName, ImageFormat.Jpeg); bm.Dispose(); return true ; } |
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
原文鏈接:https://blog.csdn.net/xiaouncle/article/details/54883327