c#裁剪圖片后使用zxing生成二維碼示例分享
/// <summary>
/// 生成二維碼
/// </summary>
/// <param name="fileName">生成二維碼路徑</param>
/// <param name="url">生成的內(nèi)容</param>
/// <param name="width">二維碼寬</param>
/// <param name="height">二維碼高</param>
/// <param name="userFace">需生成的Logo圖片</param>
/// <returns></returns>
private Bitmap GetCodeImgUrl(string fileName, string url, int width, int height, string userFace)
{
BarcodeWriter writer = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Renderer = new BitmapRenderer
{
Foreground = Color.Black
},
Options = new ZXing.QrCode.QrCodeEncodingOptions
{
DisableECI = true,
Height = height,
Width = width,
Margin = 0,
CharacterSet = "UTF-8",
ErrorCorrection = ErrorCorrectionLevel.M
}
};
Bitmap bitmap = writer.Write(url);
if (!string.IsNullOrEmpty(userFace))
{
Bitmap bits = (System.Drawing.Bitmap)System.Drawing.Image.FromFile(userFace);
if (bits != null)
{
//剪裁一個(gè)80*80的Logo圖片
ImageCut img = new ImageCut(0, 0, 80, 80);
System.Drawing.Bitmap icon = img.KiCut(bits);
//userFace_b.jpg是一個(gè)邊框的圖片
Bitmap bits2 = new System.Drawing.Bitmap((System.Drawing.Bitmap)System.Drawing.Image.FromFile(Application.StartupPath + "/user/userFace_b.jpg"), 84, 84);
if (icon != null)
{
try
{
//畫了2個(gè)邊框,一個(gè)是logo,一個(gè)在logo周圍加了一個(gè)邊框
using (var graphics = System.Drawing.Graphics.FromImage(bitmap))
{
graphics.DrawImage(bits2, (bitmap.Width - bits2.Width) / 2, (bitmap.Height - bits2.Height) / 2);
graphics.DrawImage(icon, (bitmap.Width - icon.Width) / 2, (bitmap.Height - icon.Height) / 2);
}
}
catch (Exception ex)
{
}
finally
{
icon.Dispose();
GC.Collect();
}
}
bitmap.Save(fileName, ImageFormat.Jpeg);
}
}
return bitmap;
}
public class ImageCut
{
/// <summary>
/// 剪裁 -- 用GDI+
/// </summary>
/// <param name="b">原始Bitmap</param>
/// <param name="StartX">開始坐標(biāo)X</param>
/// <param name="StartY">開始坐標(biāo)Y</param>
/// <param name="iWidth">寬度</param>
/// <param name="iHeight">高度</param>
/// <returns>剪裁后的Bitmap</returns>
public Bitmap KiCut(Bitmap b)
{
if (b == null)
{
return null;
}
int w = b.Width;
int h = b.Height;
int intWidth = 0;
int intHeight = 0;
if (h * Width / w > Height)
{
intWidth = Width;
intHeight = h * Width / w;
}
else if (h * Width / w < Height)
{
intWidth = w * Height / h;
intHeight = Height;
}
else
{
intWidth = Width;
intHeight = Height;
}
Bitmap bmpOut_b = new System.Drawing.Bitmap(b, intWidth, intHeight);
w = bmpOut_b.Width;
h = bmpOut_b.Height;
if (X >= w || Y >= h)
{
return null;
}
if (X + Width > w)
{
Width = w - X;
}
else
{
X = (w-Width) / 2;
}
if (Y + Height > h)
{
Height = h - Y;
}
try
{
Bitmap bmpOut = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmpOut);
g.DrawImage(bmpOut_b, new Rectangle(0, 0, Width, Height), new Rectangle(X, Y, Width, Height), GraphicsUnit.Pixel);
g.Dispose();
return bmpOut;
}
catch
{
return null;
}
}
public int X = 0;
public int Y = 0;
public int Width = 120;
public int Height = 120;
public ImageCut(int x, int y, int width, int heigth)
{
X = x;
Y = y;
Width = width;
Height = heigth;
}
}
private void btnSubmit_Click(object sender, EventArgs e)
{
string UserId = "1245460396";
string curFilePath = "/user/";
string curFileName_b = "DimensionalPig_" + UserId + "_b";
string path = Application.StartupPath + curFilePath;
if (Directory.Exists(path) == false)//如果不存在就創(chuàng)建file文件夾
{
Directory.CreateDirectory(path);
}
string fileName_b = Application.StartupPath + curFilePath + "/" + curFileName_b + ".jpg";//獲得上傳文件名
string UserUrl = string.Format("http://www.dbjr.com.cn/u{0}", UserId.Trim());
string userFace_b = Application.StartupPath + "/user/" + UserId + "_b.jpg";
Bitmap bitmap_b = GetCodeImgUrl(fileName_b.Replace("_b.", "_b_ewm."), UserUrl, 400, 400, userFace_b);
this.p.Image =(System.Drawing.Image)bitmap_b;
this.p.Image.Save(fileName_b.Replace("_b.", "_b_ewm."));
相關(guān)文章
C#使用Region對(duì)圖形區(qū)域構(gòu)造和填充的方法
這篇文章主要介紹了C#使用Region對(duì)圖形區(qū)域構(gòu)造和填充的方法,實(shí)例分析了Region類圖形操作的相關(guān)技巧,需要的朋友可以參考下2015-06-06Unity實(shí)現(xiàn)人物平滑轉(zhuǎn)身
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)人物平滑轉(zhuǎn)身,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01C#簡單實(shí)現(xiàn)發(fā)送socket字符串
這篇文章主要為大家詳細(xì)介紹了C#簡單實(shí)現(xiàn)socket字符串發(fā)送,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09C#實(shí)現(xiàn)定時(shí)任務(wù)Task Scheduler的示例代碼
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)定時(shí)任務(wù)Task Scheduler的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02