C#實(shí)現(xiàn)在底圖上動(dòng)態(tài)生成文字和圖片
本文主要記錄在圖片上動(dòng)態(tài)的生成需要添加的文字和把指定的圖片加到底圖上,直接上代碼
/// <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類的新實(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);
}
希望對(duì)需要這方面操作的朋友有所幫助。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C#教程
下一篇:【C#基礎(chǔ)】Substring截取字符串的方法小結(jié)(推薦)
本文標(biāo)題:C#實(shí)現(xiàn)在底圖上動(dòng)態(tài)生成文字和圖片
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/4751.html
您可能感興趣的文章
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10C#停止線程的方法
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新聞效果的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)Winform中打開(kāi)網(wǎng)頁(yè)頁(yè)面的方法
- 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
- 01-10C#自定義簽名章實(shí)現(xiàn)方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法


