var key = "http://www.xxx.xyz";
int width = 400;
int height = 400;
int pointX = 100;
int pointY = 150;
//绘制 二维码
EncodingOptions options = new QrCodeEncodingOptions()
{
DisableECI = true,
CharacterSet = "UTF-8",
Width = width,
Height = height,
Margin = 1,
};
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
writer.Options = options;
//二维码图片
Bitmap bitmap = writer.Write(key);
//背景 我暂时用白底的位图
Bitmap b1 = new Bitmap(600, 700);
Graphics g1 = Graphics.FromImage(b1); //创建g1的Graphics
g1.FillRectangle(Brushes.White, new Rectangle(0, 0, 600, 700));
g1.DrawImage(bitmap, pointX, pointY, width, height);
string avatar = "http://img.yohao.xyz//images/181019235249.jpeg";
//从链接地址中获取图片
Image avatarImage = ImageFromWebUrl(avatar);
//将头像圆形处理
var avatarBitmap = CirclePhoto(avatarImage, 100);
//绘制头像
g1.DrawImage(avatarBitmap, 40, 40, 100, 100);
var emSize = 20F;
var nickName = "我是一个优雅的小码弄";
SolidBrush solidBrush = new SolidBrush(Color.CadetBlue);
//绘制名称
g1.DrawString(nickName, new Font("黑体", emSize), solidBrush, new PointF(150, 60));
//画底部文字
var tip1 = "躺着也能赚钱的购物商城";
var tip2 = "扫一扫上面的二维码进入商城";
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
g1.DrawString(tip1, new Font("黑体", 18F), new SolidBrush(Color.FromArgb(86,78,78)), new PointF(300, 570), sf);
g1.DrawString(tip2, new Font("黑体", 18F), new SolidBrush(Color.FromArgb(86, 78, 78)), new PointF(300, 610), sf);
g1.Dispose();
byte[] imgByte = BitmapToBytes(b1);
var resp = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(imgByte)
};
resp.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");
return resp;
这里我是用webapi 写的 直接返回一张图片。
public byte[] BitmapToBytes(Bitmap Bitmap)
{
MemoryStream ms = new MemoryStream();
Bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] bytes = ms.GetBuffer();
ms.Close();
return bytes;
}
public Stream StreamFromWebUrl(string url)
{
WebRequest webRequest = WebRequest.Create(url);
HttpWebRequest request = webRequest as HttpWebRequest;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string encoding = response.ContentEncoding;
return response.GetResponseStream();
}
-从url中获取Image
///
/// 从url中获取Image
///
///
///
public Image ImageFromWebUrl(string url)
{
Stream sm = StreamFromWebUrl(url);
return Image.FromStream(sm);
}
-将图片转换层圆形
///
/// 将图片转换层圆形
///
/// Image 图片
/// 图片的尺寸
///
public Bitmap CirclePhoto(Image file, int size)
{
using (Image i = file)
{
Bitmap b = new Bitmap(size, size);
using (Graphics g = Graphics.FromImage(b))
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.DrawImage(i, 0, 0, b.Width, b.Height);
int r = Math.Min(b.Width, b.Height) / 2;
PointF c = new PointF(b.Width / 2.0F, b.Height / 2.0F);
for (int h = 0; h < b.Height; h++)
for (int w = 0; w < b.Width; w++)
if ((int)Math.Pow(r, 2) < ((int)Math.Pow(w * 1.0 - c.X, 2) + (int)Math.Pow(h * 1.0 - c.Y, 2)))
{
b.SetPixel(w, h, Color.Transparent);
}
//画背景色圆
using (Pen p = new Pen(System.Drawing.SystemColors.Control))
g.DrawEllipse(p, 0, 0, b.Width, b.Height);
}
return b;
}
}