C#生成圆角缩略图
private void MakeRoundedThumbnails(Stream fileStream, string filePath, string newFileName, int newWidth, int newHeight, ref string newSaveFilePath)
{
System.Drawing.Image myImage = System.Drawing.Image.FromStream(fileStream);
//先将图片处理为圆角
myImage = DrawTransparentRoundCornerImage(myImage);
//取得图片大小
System.Drawing.Size mySize = new Size(newWidth, newHeight);
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(mySize.Width, mySize.Height);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空一下画布
g.Clear(Color.Transparent);
//在指定位置画图
g.DrawImage(myImage, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, myImage.Width, myImage.Height),
System.Drawing.GraphicsUnit.Pixel);
//保存缩略图,强制保存为png格式,否则圆角部分无法呈现透明
Stream newFileStream = new MemoryStream();
bitmap.Save(newFileStream, ImageFormat.Png);
//此处为上传操作,可根据自己需要TODO不同
newSaveFilePath = filePath + "/" + newFileName;
FtpUpload ftpUpload = new FtpUpload("UploadFtpServer", newSaveFilePath);
newFileStream.Position = 0;
bool flag = ftpUpload.UploadFile(newFileStream);
newFileStream.Dispose();
g.Dispose();
myImage.Dispose();
bitmap.Dispose();
}
//图片处理为圆角
public static System.Drawing.Image DrawTransparentRoundCornerImage(System.Drawing.Image image)
{
Bitmap bm = new Bitmap(image.Width, image.Height);
Graphics g = Graphics.FromImage(bm);
g.FillRectangle(Brushes.Transparent, new Rectangle(0, 0, image.Width, image.Height));
using (System.Drawing.Drawing2D.GraphicsPath path = CreateRoundedRectanglePath(new Rectangle(0, 0, image.Width, image.Height), image.Width / 10))
{
g.SetClip(path);
}
g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
g.Dispose();
return bm;
}
//设置图片四个边角弧度
private static System.Drawing.Drawing2D.GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
{
System.Drawing.Drawing2D.GraphicsPath roundedRect = new System.Drawing.Drawing2D.GraphicsPath();
roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
roundedRect.CloseFigure();
return roundedRect;
}
浙公网安备 33010602011771号