翻譯|使用教程|編輯:李顯亮|2021-03-10 09:45:48.723|閱讀 493 次
概述:圖像中的水印通常用于聲明內容的所有權。對于這種情況,本文介紹如何使用C#以編程方式向圖像添加水印。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
圖像中的水印通常用于聲明內容的所有權。另一方面,對圖像加水印,以避免未經授權的使用或偽造。在各種情況下,可能需要在Web或桌面應用程序中自動執行水印功能。
對于這種情況,本文介紹如何使用C#以編程方式向圖像添加水印。當您需要一次性為一批圖像加水印時,這也可能很有用。
為了向圖像添加水印,我們將使用Aspose.Imaging。它是功能強大的圖像處理API,支持多種圖像格式。此外,API使您更輕松地操作圖像。還沒使用過的朋友可以點擊下載最新版
以下是使用C#向圖像添加水印的步驟。
以下代碼示例顯示了如何在C#中向圖像添加水印。
// Load an existing PNG image using (Image image = Image.Load("image.png")) { // Declare a String object with Watermark Text string theString = "This is watermark"; // Create and initialize an instance of Graphics class and Initialize an object of SizeF to store image Size Graphics graphics = new Graphics(image); SizeF sz = graphics.Image.Size; // Creates an instance of Font, initialize it with Font Face, Size and Style Font font = new Font("Times New Roman", 20, FontStyle.Bold); // Create an instance of SolidBrush and set its various properties SolidBrush brush = new SolidBrush(); brush.Color = Color.Red; brush.Opacity = 0; // Initialize an object of StringFormat class and set its various properties StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces; // Draw the string on Image Save output to disk graphics.DrawString(theString, font, brush, 0, 0, format); image.Save("watermarked.jpg"); }
在各種情況下,水印對角地應用于圖像。為此,Aspose.Imaging for .NET允許以特定角度轉換水印文本。以下是向圖像添加對角水印的步驟。
下面的代碼示例演示如何使用C#向圖像添加對角水印。
// Load an existing PNG image using (Image image = Image.Load("Image.png")) { // Declare a String object with Watermark Text string theString = "45 Degree Rotated Text"; // Create and initialize an instance of Graphics class and Initialize an object of SizeF to store image Size Graphics graphics = new Graphics(image); SizeF sz = graphics.Image.Size; // Creates an instance of Font, initialize it with Font Face, Size and Style Font font = new Font("Times New Roman", 20, FontStyle.Bold); // Create an instance of SolidBrush and set its various properties SolidBrush brush = new SolidBrush(); brush.Color = Color.Red; brush.Opacity = 0; // Initialize an object of StringFormat class and set its various properties StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces; // Create an object of Matrix class for transformation Matrix matrix = new Matrix(); // First a translation then a rotation matrix.Translate(sz.Width / 2, sz.Height / 2); matrix.Rotate(-45.0f); // Set the Transformation through Matrix graphics.Transform = matrix; // Draw the string on Image Save output to disk graphics.DrawString(theString, font, brush, 0, 0, format); image.Save("watermarked.png"); }
如果你想試用Aspose的全部完整功能,可 聯系在線客服獲取30天臨時授權體驗。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn