這篇文章主要介紹了C#如何實(shí)現(xiàn)在底圖上動(dòng)態(tài)生成文字和圖片,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
成都創(chuàng)新互聯(lián)公司主營(yíng)棲霞網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,app軟件開(kāi)發(fā),棲霞h5小程序制作搭建,棲霞網(wǎng)站營(yíng)銷(xiāo)推廣歡迎棲霞等地區(qū)企業(yè)咨詢(xún)
代碼:
/// <summary> /// 在底圖上畫(huà)指定路徑的圖片 /// </summary> /// <param name="g">畫(huà)板實(shí)例</param> /// <param name="path">圖片路徑</param> /// <param name="totalWidth">畫(huà)區(qū)總長(zhǎng)度</param> /// <param name="totalHeight">畫(huà)區(qū)總高度</param> /// <param name="px">起點(diǎn)X坐標(biāo)</param> /// <param name="py">起點(diǎn)Y坐標(biāo)</param> private void FontPic(ref Graphics g, string path, int totalWidth, int totalHeight, int px, int py) { if (File.Exists(path)) { var pImg = Image.FromFile(path); //如果圖片大于畫(huà)布區(qū)域,則縮小 if (totalHeight < pImg.Height && totalWidth < pImg.Width) { Image newPic = GetReducedImage(pImg, totalWidth, totalHeight); if (newPic != null) { DrawPic(ref g, totalWidth, totalHeight, px, py, newPic); } } else if (totalHeight < pImg.Height && totalWidth >= pImg.Width) { Image newPic = GetReducedImage(pImg, pImg.Width, totalHeight); if (newPic != null) { DrawPic(ref g, totalWidth, totalHeight, px, py, newPic); } } else if (totalHeight >= pImg.Height && totalWidth < pImg.Width) { Image newPic = GetReducedImage(pImg, totalWidth, pImg.Height); if (newPic != null) { DrawPic(ref g, totalWidth, totalHeight, px, py, newPic); } } else { DrawPic(ref g, totalWidth, totalHeight, px, py, pImg); } } } /// <summary> /// 在圖上畫(huà)圖片 /// </summary> /// <param name="g">畫(huà)板實(shí)例</param> /// <param name="totalWidth">畫(huà)區(qū)總長(zhǎng)度</param> /// <param name="totalHeight">畫(huà)區(qū)總高度</param> /// <param name="px">起點(diǎn)X坐標(biāo)</param> /// <param name="py">起點(diǎn)Y坐標(biāo)</param> /// <param name="pImg">要畫(huà)的圖片實(shí)例</param> private void DrawPic(ref Graphics g, int totalWidth, int totalHeight, int px, int py, Image pImg) { px += GetValue(totalWidth, pImg.Width); py += GetValue(totalHeight, pImg.Height); g.DrawImage(new Bitmap(pImg, new Size(GetSize(totalWidth, pImg.Width), GetSize(totalHeight, pImg.Height))), new Rectangle(px, py, totalWidth, totalHeight), 0, 0, totalWidth, totalHeight, GraphicsUnit.Pixel); } /// <summary> /// 生成縮略圖重載方法1,返回縮略圖的Image對(duì)象 /// </summary> /// <param name="width">縮略圖的寬度</param> /// <param name="height">縮略圖的高度</param> /// <returns>縮略圖的Image對(duì)象</returns> public Image GetReducedImage(Image resourceImage, int width, int height) { try { Image data = null; //用指定的大小和格式初始化Bitmap類(lèi)的新實(shí)例 using (Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb)) { //從指定的Image對(duì)象創(chuàng)建新Graphics對(duì)象 using (Graphics graphics = Graphics.FromImage(bitmap)) { //清除整個(gè)繪圖面并以透明背景色填充 //graphics.Clear(Color.Transparent); //在指定位置并且按指定大小繪制原圖片對(duì)象 graphics.DrawImage(resourceImage, new Rectangle(0, 0, width, height)); } data = new Bitmap(bitmap); } return data; } catch (Exception e) { throw e; } } /// <summary> /// 比較兩個(gè)值,得到給到給定值(判斷是否越界) /// </summary> /// <param name="total">總長(zhǎng)度</param> /// <param name="width">指定長(zhǎng)度</param> /// <returns></returns> public int GetSize(int total, int width) { if (total > width) { return width; } else { return total; } } /// <summary> /// 更加傳入的值計(jì)算得到新值(計(jì)算點(diǎn)坐標(biāo)) /// </summary> /// <param name="total">總長(zhǎng)度</param> /// <param name="width">指定長(zhǎng)度</param> /// <returns></returns> private int GetValue(int total, int width) { return (total - width) / 2; } /// <summary> /// 在圖片上畫(huà)出文字 /// </summary> /// <param name="g">圖片對(duì)象</param> /// <param name="pointX">文字x坐標(biāo)</param> /// <param name="pointY">文字y坐標(biāo)</param> /// <param name="word">文字內(nèi)容</param> /// <param name="textWidth">文本寬度</param> /// <param name="textHeight">文本高度</param> private static void DrawStringWord(Graphics g, int pointX, int pointY, string word, int textWidth, int textHeight, int fontSize = 30) { Font font = new Font("微軟雅黑", fontSize, (FontStyle.Regular)); RectangleF textArea = new RectangleF(pointX, pointY, textWidth, textHeight); Brush brush = new SolidBrush(Color.Black); g.DrawString(word, font, brush, textArea); }
C#是一個(gè)簡(jiǎn)單、通用、面向?qū)ο蟮木幊陶Z(yǔ)言,它由微軟Microsoft開(kāi)發(fā),繼承了C和C++強(qiáng)大功能,并且去掉了一些它們的復(fù)雜特性,C#綜合了VB簡(jiǎn)單的可視化操作和C++的高運(yùn)行效率,以其強(qiáng)大的操作能力、優(yōu)雅的語(yǔ)法風(fēng)格、創(chuàng)新的語(yǔ)言特性和便捷的面向組件編程從而成為.NET開(kāi)發(fā)的首選語(yǔ)言,但它不適用于編寫(xiě)時(shí)間急迫或性能非常高的代碼,因?yàn)镃#缺乏性能極高的應(yīng)用程序所需要的關(guān)鍵功能。
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“C#如何實(shí)現(xiàn)在底圖上動(dòng)態(tài)生成文字和圖片”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
文章題目:C#如何實(shí)現(xiàn)在底圖上動(dòng)態(tài)生成文字和圖片
文章鏈接:http://m.rwnh.cn/article10/jcjsdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、品牌網(wǎng)站制作、網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、面包屑導(dǎo)航、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)