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);
計算居中座標便可c#
//畫名字 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; } } }
/// <summary> /// 毫米轉爲像素(注:dpi分水平和垂直,獲取方法爲獲得 Graphics 的實例化對象 g,調用g.DpiX、g.DpiY) /// </summary> /// <param name="mm">毫米</param> /// <param name="fDPI">分辨率(水平/垂直)</param> /// <returns></returns> public static float MillimetersToPixel(float mm, float fDPI) { //毫米轉像素:mm * dpi / 25.4 return (float)Math.Round((mm * fDPI / 25.4f), 2); } /// <summary> /// 像素轉爲毫米(注:dpi分水平和垂直,獲取方法爲獲得 Graphics 的實例化對象 g,調用g.DpiX、g.DpiY) /// </summary> /// <param name="px">像素</param> /// <param name="fDPI">分辨率(水平/垂直)</param> /// <returns></returns> public static float PixelToMillimeters(float px, float fDPI) { //像素轉毫米:px * 25.4 / dpi return (float)Math.Round(((px * 25.4f) / fDPI), 2); ; } /// <summary> /// 英寸到像素 /// </summary> /// <param name="inches"></param> /// <returns></returns> public static float InchesToPixels(float inches, float fDPI) { return (float)Math.Round(inches * fDPI, 2); } /// <summary> /// 像素到英寸 /// </summary> /// <param name="px"></param> /// <returns></returns> public static float PixelsToInches(float px, float fDPI) { return (float)Math.Round(px / fDPI, 2); } /// <summary> /// 毫米到英寸 /// </summary> /// <param name="mm"></param> /// <returns></returns> public static float MillimetersToInches(float mm) { return (float)Math.Round(mm / 25.4f, 2); } /// <summary> /// 英寸到毫米 /// </summary> /// <param name="mm"></param> /// <returns></returns> public static float InchesToMillimeters(float Inches) { return (float)Math.Round(Inches * 25.4f, 2); }