C#影像處理(1):在圖片上加文字和改變文字的方向

來源:互聯網
上載者:User

標籤:style   blog   color   os   io   檔案   for   ar   

C#在圖片上加文字,代碼如下:

 1         /// <summary> 2         /// 圖片上方加文字,文字將會被180度反轉 3         /// </summary> 4         /// <param name="Img">待處理圖片</param> 5         /// <param name="WriteString">寫入的字串</param> 6         /// <param name="UpMargin">180度反轉後文字頂部距離上邊緣距離</param> 7         /// <param name="RightMargin">文字最左邊距離右邊緣距離</param> 8         /// <returns>Bitmap</returns> 9         public Bitmap WriteUp(Image Img, string WriteString, int UpMargin, int RightMargin)10         {11             return WriteUp(Img, WriteString, UpMargin, RightMargin, "宋體", 20);12         }13 14         /// <summary>15         /// 圖片上方加文字,文字將會被180度反轉16         /// </summary>17         /// <param name="Img">待處理圖片</param>18         /// <param name="WriteString">寫入的字串</param>19         /// <param name="UpMargin">180度反轉後文字頂部距離上邊緣距離</param>20         /// <param name="RightMargin">文字最左邊距離右邊緣距離</param>21         /// <param name="FontType">字型類型</param>22         /// <param name="FontSize">字型大小</param>23         /// <returns></returns>24         public Bitmap WriteUp(Image Img, string WriteString, int UpMargin, int RightMargin, string FontType, int FontSize)25         {26             //擷取圖片寬高27             int Width = Img.Width;28             int Height = Img.Height;29             //擷取圖片水平和垂直的解析度30             float dpiX = Img.HorizontalResolution;31             float dpiY = Img.VerticalResolution;32             //建立一個位元影像檔案33             Bitmap BitmapResult = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);34             //設定位元影像檔案的水平和垂直解析度  與Img一致35             BitmapResult.SetResolution(dpiX, dpiY);36             //在位元影像檔案上填充一個矩形框37             Graphics Grp = Graphics.FromImage(BitmapResult);38             System.Drawing.Rectangle Rec = new System.Drawing.Rectangle(0, 0, Width, Height);39             //向矩形框內填充Img40             Grp.DrawImage(Img, 0, 0, Rec, GraphicsUnit.Pixel);41 42 43             //平移Graphics對象44             Grp.TranslateTransform(Width - RightMargin, UpMargin);45             //設定Graphics對象的輸出角度以改變文字方向46             Grp.RotateTransform(180);47             //設定文字填充顏色48             Brush brush = Brushes.Black;49             //旋轉顯示文字50             Grp.DrawString(WriteString, new Font(FontType, FontSize, GraphicsUnit.Pixel), brush, 0, 0);51             //恢複全域變換矩陣52             Grp.ResetTransform();53             Grp.Dispose();54             GC.Collect();55             return BitmapResult;56         }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.