This article describes the functions of uploading pictures, saving pictures, adding watermarks, and generating thumbnails implemented in C#. Share it for your reference, as follows:
With the popularity of mobile devices, the need to process pictures, videos, etc. has become increasingly basic. Here is the storage of pictures.
Uploading images must use form submission method. I only know this method. If you know other methods, please leave a message.
It is best to place the three functions of saving pictures, adding watermarks and generating thumbnails in separate methods to minimize coupling and improve the degree of code reuse. In addition, we should try our best to achieve the uniqueness of the method functions when writing code normally.
Front Desk Code:
<form method="POST" enctype="multipart/form-data" action=""> <table> <tr> <td>func:</td> <td><input type="text" name="func"/></td> </tr> <tr> <td>userId:</td> <td><input type="text" name="userId"/></td> </tr> <tr> <td>avatar:</td> <td><input type="file" name="icon"/></td> </tr> <tr> <td>Watermark:</td> <td><input type="text" name="waterMark"/></td> </tr> </table> <input type="submit" value="submit"/> </form>
Background code:
private string UploadImage(HttpContext context) { try { stream = ["icon"].InputStream; //The returned image path can be stored in the database string imageUrl = SaveImage(stream, "Icon", "Cassipede"); string thumbnailImageUrl = SaveThumbnailImage(stream, "Icon"); string thumbnailImageUrlWithWatermark = SaveThumbnailImage(["AttachmentsDirectory"] + imageUrl, "Icon"); return "Uploaded successfully!"; } catch (Exception ex) { return "Upload failed!"; } } private string SaveImage(Stream stream, string folderName, string waterMark) { try { string fileName = () + ".jpg"; string path = ["AttachmentsDirectory"]; path = (path, folderName + "\\" + + "\\" + + "\\" + + "\\"); string imageUrl = "/" + folderName + "/" + + "/" + + "/" + + "/"; if (!(waterMark)) { Image imgSource = (stream); AddWatermarkAndSave(path, fileName, waterMark, imgSource, - 300, 10, , new Font("Song-style", 40)); } else { byte[] buffer = new byte[]; (buffer, 0, ); if (!(path)) { (path); } fs = new (path + fileName, , ); (buffer, 0, ); (); (); } return imageUrl + fileName; } catch (Exception ex) { return ""; } } private string SaveThumbnailImage(Stream stream, string folderName) { try { string fileName = () + ".jpg"; string path = ["AttachmentsDirectory"]; path = (path, folderName + "\\" + + "\\" + + "\\" + + "\\"); string imageUrl = "/" + folderName + "/" + + "/" + + "/" + + "/"; myCallback = new (GetFalse); //The data source comes from Stream Image image = (stream); thumbnailImage = (64, 64, myCallback, ); (path + fileName); (); return imageUrl + fileName; } catch (Exception ex) { return ""; } } private string SaveThumbnailImage(string originalFileName, string folderName) { try { string fileName = () + ".jpg"; string path = ["AttachmentsDirectory"]; path = (path, folderName + "\\" + + "\\" + + "\\" + + "\\"); string imageUrl = "/" + folderName + "/" + + "/" + + "/" + + "/"; myCallback = new (GetFalse); //The data source comes from File Image image = (originalFileName); thumbnailImage = (64, 64, myCallback, ); (path + fileName); (); return imageUrl + fileName; } catch (Exception ex) { return ""; } } private bool GetFalse() { return false; } /// <summary> /// Picture with text watermark/// </summary> /// <param name="fileName"> </param> /// <param name="text">Watermark text, if it is multiple lines, separated by semicolons</param>/// <param name="img">Picture</param>/// <param name="paddingTop">top margin</param>/// <param name="paddingLeft">Left margin</param>/// <param name="textColor">Text Color</param>/// <param name="textFont">Font</param>/// <param name="path">Save address</param>/// <returns></returns> private bool AddWatermarkAndSave(string path, string fileName, string text, Image img, int paddingTop, int paddingLeft, Color textColor, Font textFont) { text = text + ";" + "Current time:" + ("yyyy-MM-dd HH:mm"); if (!(path)) { (path); } textFont = new Font("Song-style", 19); Bitmap bm = new Bitmap(img); g = (bm); b = new SolidBrush(textColor); string[] str = (';'); for (int i = 0; i < ; i++) (str[i], textFont, b, paddingLeft, paddingTop + 33 * i); (); (path + fileName, ); (); return true; }
For more information about C# related content, please check out the topic of this site:Summary of C# picture operation skills》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming》
I hope this article will be helpful to everyone's C# programming.