c clipped the picture and used zxing to generate the QR code sample to share
- 2020-06-07 05:11:32
- OfStack
/// <summary>
/// generate 2 D code
/// </summary>
/// <param name="fileName"> generate 2 D code path </param>
/// <param name="url"> Generated content </param>
/// <param name="width">2 D yards wide </param>
/// <param name="height">2 High dimension code </param>
/// <param name="userFace"> To be generated Logo The picture </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)
{
// clipping 1 a 80*80 the Logo The picture
ImageCut img = new ImageCut(0, 0, 80, 80);
System.Drawing.Bitmap icon = img.KiCut(bits);
//userFace_b.jpg is 1 A border image
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
{
// drew 2 A border, 1 One is logo,1 in logo Around with 1 A border
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>
/// clipping -- with GDI+
/// </summary>
/// <param name="b"> The original Bitmap</param>
/// <param name="StartX"> To coordinate X</param>
/// <param name="StartY"> To coordinate Y</param>
/// <param name="iWidth"> The width of the </param>
/// <param name="iHeight"> highly </param>
/// <returns> After clipping 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)// If it doesn't exist, create it file folder
{
Directory.CreateDirectory(path);
}
string fileName_b = Application.StartupPath + curFilePath + "/" + curFileName_b + ".jpg";// Get the upload file name
string UserUrl = string.Format("https://www.ofstack.com/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."));