C# GDI+开发手记
2021-05-03 00:26
标签:旋转 clip eal web alias collect 矩阵 add map 计算居中坐标即可 C# GDI+开发手记 标签:旋转 clip eal web alias collect 矩阵 add map 原文地址:http://www.cnblogs.com/leestar54/p/7760070.html
画字体
文字区域内居中换行
文字在整个画布中居中
画直线
画圆形头像
压缩保存图片
缩放旋转
单位换算创建画布
Bitmap image = new Bitmap(640, 1136, PixelFormat.Format32bppArgb);
//获得画布,设置高质量抗锯齿相关参数
Graphics g = Graphics.FromImage(image);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
//g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
画字体
//获得字体
string path = @"c:\HUAKANGZUOJINHEIW8.TTF";
//读取字体文件
PrivateFontCollection pfc = new PrivateFontCollection();
pfc.AddFontFile(path);
//实例化字体
System.Drawing.Font printFont = new System.Drawing.Font(pfc.Families[0], 50, FontStyle.Regular);
System.Drawing.Font printFont2 = new System.Drawing.Font(pfc.Families[0], 30, FontStyle.Regular);
System.Drawing.Font printFont3 = new System.Drawing.Font("微软雅黑", 20, FontStyle.Regular);
g.DrawString("永恒钻石", font, Brushes.Black, 546, 186);
文字区域内居中换行
//字体在区域居中
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
Rectangle stringRect = new Rectangle(0, 50, 640, 276);
g.DrawString(name, printFont, Brushes.Black, stringRect, sf);
文字在整个画布中居中
//画名字
SizeF str_size = g.MeasureString(name, printFont);
g.DrawString(name, printFont, Brushes.White, (int)(320 - (str_size.Width / 2)), 285);
Pen p = new Pen(Color.White, 5);
画直线
Pen p = new Pen(Color.White, 5);
g.DrawLine(p, (int)(320 - (str_size.Width / 2)), 285 + str_size.Height, (int)(320 + (str_size.Width / 2)), 285 + str_size.Height);
画圆形头像
//画头像
//获得线笔
Pen pen = new Pen(Color.White, 5);
//新建个圆形路径
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(0, 0, 300, 300);
//先获得头像
Bitmap avator = new Bitmap(GetHttpBitmap(txurl), 300, 300);
//再新建个头像画布,把头像画上去
Bitmap avator_bp = new Bitmap(avator.Width, avator.Height);
using (Graphics gg = Graphics.FromImage(avator_bp))
{
//圆形剪辑路径区域,类似于遮罩
gg.SetClip(gp);
gg.DrawImage(avator, 0, 0);
gg.Dispose();
}
//画头像的圈圈
g.DrawEllipse(pen, 210, 30, 220, 220);
g.DrawImage(avator_bp, 220, 40, 200, 200);
bpp.Dispose();
bppp.Dispose();
压缩保存图片
public static ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
//唯一名称
string guid = Guid.NewGuid().ToString("N");
//设置JPG压缩级别
ImageCodecInfo jgpEncoder = Utils.GetEncoder(ImageFormat.Jpeg);
//见https://msdn.microsoft.com/en-us/library/system.drawing.imaging.encoder(v=vs.110).aspx
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 95L);
myEncoderParameters.Param[0] = myEncoderParameter;
//可以上传阿里云
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, jgpEncoder, myEncoderParameters);
}
return guid;
缩放旋转
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
namespace ImageUtils
{
public static class EditImage
{
public static Bitmap ScaleImage(Bitmap img, float scalefactor)
{
Size size = new Size((int)(img.Width * scalefactor), (int)(img.Height * scalefactor));
Bitmap bp = new Bitmap(img, size);
return bp;
}
public static Bitmap RotateImage(Bitmap bmp, float angle, Color bkColor)
{
int w = bmp.Width + 2;
int h = bmp.Height + 2;
PixelFormat pf;
if (bkColor == Color.Transparent)
{
pf = PixelFormat.Format32bppArgb;
}
else
{
pf = bmp.PixelFormat;
}
Bitmap tmp = new Bitmap(w, h, pf);
Graphics g = Graphics.FromImage(tmp);
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.Clear(bkColor);
g.DrawImageUnscaled(bmp, 1, 1);
g.Dispose();
GraphicsPath path = new GraphicsPath();
path.AddRectangle(new RectangleF(0f, 0f, w, h));
Matrix mtrx = new Matrix();
mtrx.Rotate(angle);//矩阵变换
RectangleF rct = path.GetBounds(mtrx);//获取边界
Bitmap dst = new Bitmap((int)rct.Width, (int)rct.Height, pf);
g = Graphics.FromImage(dst);
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.Clear(bkColor);
g.TranslateTransform(-rct.X, -rct.Y);//因为rct的值为负
g.RotateTransform(angle);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;//.HighQualityBilinear;
g.DrawImageUnscaled(tmp, 0, 0);
g.Dispose();
tmp.Dispose();
return dst;
}
}
}
单位换算
///