实现图片圆角
今天特意找了个关于图片去圆角的文章
找到http://www.cnblogs.com/lovecherry/archive/2006/05/17/402541.html
提供了主要的关键代码:
我稍微的去了些功能 只留下了一个方法 来处理 图片圆角的功能
嘿嘿
其实其他的几个方法还没来得及看
创建一个类
COPY进去
我使用了一个笨方法来实现 一个图片一次性的将四个圆角全部切出来
这个方法太笨了
我试过这么做
但是 图片出来后 面目全非
哪位朋友可以帮我看看 有没有更好点的方法
找到http://www.cnblogs.com/lovecherry/archive/2006/05/17/402541.html
提供了主要的关键代码:
我稍微的去了些功能 只留下了一个方法 来处理 图片圆角的功能
嘿嘿
其实其他的几个方法还没来得及看
创建一个类
COPY进去
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
/// <summary>
/// Class1 的摘要说明
/// </summary>
public class MyGDI
{
//创建 圆角图片的方法
方法参数的说明
public static void CreateRoundedCorner(string sSrcFilePath, string sDstFilePath, string sCornerLocation)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(sSrcFilePath);
Graphics g = Graphics.FromImage(image);
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality;
Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
GraphicsPath rectPath = CreateRoundRectanglePath(rect, image.Width / 10, sCornerLocation); //构建圆角外部路径
Brush b = new SolidBrush(Color.White);//圆角背景白色
g.DrawPath(new Pen(b), rectPath);
g.FillPath(b, rectPath);
g.Dispose();
image.Save(sDstFilePath, ImageFormat.Jpeg);
image.Dispose();
}
private static GraphicsPath CreateRoundRectanglePath(Rectangle rect, int radius, string sPosition)
{
GraphicsPath rectPath = new GraphicsPath();
switch (sPosition)
{
case "TopLeft":
{
rectPath.AddArc(rect.Left, rect.Top, radius * 2, radius * 2, 180, 90);
rectPath.AddLine(rect.Left, rect.Top, rect.Left, rect.Top + radius);
break;
}
case "TopRight":
{
rectPath.AddArc(rect.Right - radius * 2, rect.Top, radius * 2, radius * 2, 270, 90);
rectPath.AddLine(rect.Right, rect.Top, rect.Right - radius, rect.Top);
break;
}
case "BottomLeft":
{
rectPath.AddArc(rect.Left, rect.Bottom - radius * 2, radius * 2, radius * 2, 90, 90);
rectPath.AddLine(rect.Left, rect.Bottom - radius, rect.Left, rect.Bottom);
break;
}
case "BottomRight":
{
rectPath.AddArc(rect.Right - radius * 2, rect.Bottom - radius * 2, radius * 2, radius * 2, 0, 90);
rectPath.AddLine(rect.Right - radius, rect.Bottom, rect.Right, rect.Bottom);
break;
}
}
return rectPath;
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
/// <summary>
/// Class1 的摘要说明
/// </summary>
public class MyGDI
{
//创建 圆角图片的方法
方法参数的说明
public static void CreateRoundedCorner(string sSrcFilePath, string sDstFilePath, string sCornerLocation)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(sSrcFilePath);
Graphics g = Graphics.FromImage(image);
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality;
Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
GraphicsPath rectPath = CreateRoundRectanglePath(rect, image.Width / 10, sCornerLocation); //构建圆角外部路径
Brush b = new SolidBrush(Color.White);//圆角背景白色
g.DrawPath(new Pen(b), rectPath);
g.FillPath(b, rectPath);
g.Dispose();
image.Save(sDstFilePath, ImageFormat.Jpeg);
image.Dispose();
}
private static GraphicsPath CreateRoundRectanglePath(Rectangle rect, int radius, string sPosition)
{
GraphicsPath rectPath = new GraphicsPath();
switch (sPosition)
{
case "TopLeft":
{
rectPath.AddArc(rect.Left, rect.Top, radius * 2, radius * 2, 180, 90);
rectPath.AddLine(rect.Left, rect.Top, rect.Left, rect.Top + radius);
break;
}
case "TopRight":
{
rectPath.AddArc(rect.Right - radius * 2, rect.Top, radius * 2, radius * 2, 270, 90);
rectPath.AddLine(rect.Right, rect.Top, rect.Right - radius, rect.Top);
break;
}
case "BottomLeft":
{
rectPath.AddArc(rect.Left, rect.Bottom - radius * 2, radius * 2, radius * 2, 90, 90);
rectPath.AddLine(rect.Left, rect.Bottom - radius, rect.Left, rect.Bottom);
break;
}
case "BottomRight":
{
rectPath.AddArc(rect.Right - radius * 2, rect.Bottom - radius * 2, radius * 2, radius * 2, 0, 90);
rectPath.AddLine(rect.Right - radius, rect.Bottom, rect.Right, rect.Bottom);
break;
}
}
return rectPath;
}
}
我使用了一个笨方法来实现 一个图片一次性的将四个圆角全部切出来
这个方法太笨了
protected void Page_Load(object sender, EventArgs e)
{
// MyGDI.
MyGDI.CreateRoundedCorner(@"C:\01.JPG", @"C:\02.JPG", "TopLeft");
MyGDI.CreateRoundedCorner(@"c:\02.jpg", @"c:\03.jpg", "TopRight");
MyGDI.CreateRoundedCorner(@"C:\03.JPG", @"C:\04.JPG", "BottomLeft");
MyGDI.CreateRoundedCorner(@"c:\04.jpg", @"c:\05.jpg", "BottomRight");
}
{
// MyGDI.
MyGDI.CreateRoundedCorner(@"C:\01.JPG", @"C:\02.JPG", "TopLeft");
MyGDI.CreateRoundedCorner(@"c:\02.jpg", @"c:\03.jpg", "TopRight");
MyGDI.CreateRoundedCorner(@"C:\03.JPG", @"C:\04.JPG", "BottomLeft");
MyGDI.CreateRoundedCorner(@"c:\04.jpg", @"c:\05.jpg", "BottomRight");
}
我试过这么做
//case "TopAll":
// {
// rectPath.AddArc(rect.Left, rect.Top, radius * 2, radius * 2, 180, 90);
// rectPath.AddLine(rect.Left, rect.Top, rect.Left, rect.Top + radius);
// rectPath.AddArc(rect.Right - radius * 2, rect.Top, radius * 2, radius * 2, 270, 90);
// rectPath.AddLine(rect.Right, rect.Top, rect.Right - radius, rect.Top);
// //rectPath.AddArc(rect.Left, rect.Bottom - radius * 2, radius * 2, radius * 2, 90, 90);
// //rectPath.AddLine(rect.Left, rect.Bottom - radius, rect.Left, rect.Bottom);
// //rectPath.AddArc(rect.Right - radius * 2, rect.Bottom - radius * 2, radius * 2, radius * 2, 0, 90);
// //rectPath.AddLine(rect.Right - radius, rect.Bottom, rect.Right, rect.Bottom);
// break;
// }
//case "BottomAll":
// {
// //rectPath.AddArc(rect.Left, rect.Top, radius * 2, radius * 2, 180, 90);
// //rectPath.AddLine(rect.Left, rect.Top, rect.Left, rect.Top + radius);
// //rectPath.AddArc(rect.Right - radius * 2, rect.Top, radius * 2, radius * 2, 270, 90);
// //rectPath.AddLine(rect.Right, rect.Top, rect.Right - radius, rect.Top);
// rectPath.AddArc(rect.Left, rect.Bottom - radius * 2, radius * 2, radius * 2, 90, 90);
// rectPath.AddLine(rect.Left, rect.Bottom - radius, rect.Left, rect.Bottom);
// rectPath.AddArc(rect.Right - radius * 2, rect.Bottom - radius * 2, radius * 2, radius * 2, 0, 90);
// rectPath.AddLine(rect.Right - radius, rect.Bottom, rect.Right, rect.Bottom);
// break;
// }
// {
// rectPath.AddArc(rect.Left, rect.Top, radius * 2, radius * 2, 180, 90);
// rectPath.AddLine(rect.Left, rect.Top, rect.Left, rect.Top + radius);
// rectPath.AddArc(rect.Right - radius * 2, rect.Top, radius * 2, radius * 2, 270, 90);
// rectPath.AddLine(rect.Right, rect.Top, rect.Right - radius, rect.Top);
// //rectPath.AddArc(rect.Left, rect.Bottom - radius * 2, radius * 2, radius * 2, 90, 90);
// //rectPath.AddLine(rect.Left, rect.Bottom - radius, rect.Left, rect.Bottom);
// //rectPath.AddArc(rect.Right - radius * 2, rect.Bottom - radius * 2, radius * 2, radius * 2, 0, 90);
// //rectPath.AddLine(rect.Right - radius, rect.Bottom, rect.Right, rect.Bottom);
// break;
// }
//case "BottomAll":
// {
// //rectPath.AddArc(rect.Left, rect.Top, radius * 2, radius * 2, 180, 90);
// //rectPath.AddLine(rect.Left, rect.Top, rect.Left, rect.Top + radius);
// //rectPath.AddArc(rect.Right - radius * 2, rect.Top, radius * 2, radius * 2, 270, 90);
// //rectPath.AddLine(rect.Right, rect.Top, rect.Right - radius, rect.Top);
// rectPath.AddArc(rect.Left, rect.Bottom - radius * 2, radius * 2, radius * 2, 90, 90);
// rectPath.AddLine(rect.Left, rect.Bottom - radius, rect.Left, rect.Bottom);
// rectPath.AddArc(rect.Right - radius * 2, rect.Bottom - radius * 2, radius * 2, radius * 2, 0, 90);
// rectPath.AddLine(rect.Right - radius, rect.Bottom, rect.Right, rect.Bottom);
// break;
// }
但是 图片出来后 面目全非
哪位朋友可以帮我看看 有没有更好点的方法