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類(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);
}
希望對(duì)需要這方面操作的朋友有所幫助。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#基于Twain協(xié)議調(diào)用掃描儀,設(shè)置多圖像輸出模式(Multi image output)
- C#實(shí)現(xiàn)掃描槍掃描二維碼并打印(實(shí)例代碼)
- 基于C#實(shí)現(xiàn)的端口掃描器實(shí)例代碼
- c#掃描圖片去黑邊(掃描儀去黑邊)
- asp.net(C#)生成Code39條形碼實(shí)例 條碼槍可以?huà)呙璩?/a>
- c# 可疑文件掃描代碼(找到木馬)(簡(jiǎn))
- C#中實(shí)現(xiàn)網(wǎng)段掃描的代碼
- c# 生成文字圖片和合并圖片的示例
- C#利用iTextSharp組件給PDF文檔添加圖片/文字水印
- C#生成Word文件(圖片、文字)
- 如何使用C#掃描并讀取圖片中的文字
相關(guān)文章
C#實(shí)現(xiàn)的簡(jiǎn)單整數(shù)四則運(yùn)算計(jì)算器功能示例
這篇文章主要介紹了C#實(shí)現(xiàn)的簡(jiǎn)單整數(shù)四則運(yùn)算計(jì)算器功能,涉及C#界面布局、事件響應(yīng)及數(shù)值運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
C#實(shí)現(xiàn)將漢字轉(zhuǎn)化為2位大寫(xiě)的16進(jìn)制Unicode的方法
這篇文章主要介紹了C#實(shí)現(xiàn)將漢字轉(zhuǎn)化為2位大寫(xiě)的16進(jìn)制Unicode的方法,分析了轉(zhuǎn)換的技巧并以實(shí)例形式給出了具體的轉(zhuǎn)換方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12
WPF運(yùn)行時(shí)替換方法實(shí)現(xiàn)mvvm自動(dòng)觸發(fā)刷新
這篇文章主要為大家詳細(xì)介紹了WPF運(yùn)行時(shí)如何實(shí)現(xiàn)setter不需要調(diào)方法就可以自動(dòng)觸發(fā)界面刷新,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04
c#檢測(cè)端口是否被占用的簡(jiǎn)單實(shí)例
這篇文章主要介紹了c#檢測(cè)端口是否被占用的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-12-12
總結(jié)C#刪除字符串?dāng)?shù)組中空字符串的幾種方法
C#中要如何才能刪除一個(gè)字符串?dāng)?shù)組中的空字符串呢?下面的文章會(huì)介紹多種方式來(lái)實(shí)現(xiàn)清除數(shù)組中的空字符串,以及在.net中將字符串?dāng)?shù)組中字符串為空的元素去除。2016-08-08
C#中判斷字符串是全角還是半角的實(shí)現(xiàn)代碼
本篇文章主要是對(duì)C#中判斷字符串是全角還是半角的實(shí)現(xiàn)代碼進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01

