C# 图片的缩放和圆角问题

来源:互联网 发布:java开发工程师薪资 编辑:程序博客网 时间:2024/06/14 04:53

        最近需要实现一个能够缩放并把图片处理成圆角 ,就像PS中的滤镜圆角效果。在网上找了好多资料,一直没找着好的解决方案,嘿嘿。

我本打算是想先用Graphic+GDI绘制出圆角矩形后,再把图片填充进去实现,但是没有成功。

        因为本人水平有限Graphic又不是很熟,所以呵呵只能从网上找资源,找解决方案。终于找着一篇给了个解决方案的博客http://www.cnblogs.com/lovecherry/archive/2006/05/17/402541.html

我根据里面给的方法写了个类,用于实现功能,不过这个功能是通过切图片得到的,并不是我当时想的先画出圆角矩形后,再填充得到的。

        但是能够实现功能啦O(∩_∩)O哈!

        背景是用白色填充,这种图片在深色北京网站中就会显现出来,所以要是想吧图片在深色网站中显示的话,就要设置一下画布颜色了。

using System;using System.Data;using System.Configuration;using System.Web;using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imaging;/// <summary>/// ImageOption 的摘要说明/// </summary>public class ImageOperation{      /// <summary>    /// 圆角生成(但是只能是一个角)    /// </summary>    /// <param name="image">源图片 Image</param>    /// <param name="roundCorner">圆角位置</param>    /// <returns>处理好的Image</returns>    public static Image CreateRoundedCorner(Image image, RoundRectanglePosition roundCorner)    {        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, roundCorner);         //圆角背景用白色填充        Brush b = new SolidBrush(Color.White);        g.DrawPath(new Pen(b), rectPath);        g.FillPath(b, rectPath);        g.Dispose();        return image;    }    /// <summary>    /// 目标图片的圆角位置    /// </summary>    public enum RoundRectanglePosition    {        /// <summary>        /// 左上角        /// </summary>        TopLeft,        /// <summary>        /// 右上角        /// </summary>        TopRight,        /// <summary>        /// 左下角        /// </summary>        BottomLeft,        /// <summary>        /// 右下角        /// </summary>        BottomRight    }    /// <summary>    /// 构建GraphicsPath路径    /// </summary>    /// <param name="rect"></param>    /// <param name="radius"></param>    /// <param name="rrPosition">图片圆角位置</param>    /// <returns>返回GraphicPath</returns>    private static GraphicsPath CreateRoundRectanglePath(Rectangle rect, int radius, RoundRectanglePosition rrPosition)    {        GraphicsPath rectPath = new GraphicsPath();        switch (rrPosition)        {            case RoundRectanglePosition.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 RoundRectanglePosition.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 RoundRectanglePosition.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 RoundRectanglePosition.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;    }    /// <summary>    /// 图片缩放    /// </summary>    /// <param name="b">源图片Bitmap</param>    /// <param name="dstWidth">目标宽度</param>    /// <param name="dstHeight">目标高度</param>    /// <returns>处理完成的图片 Bitmap</returns>    public static Bitmap ResizeBitmap(Bitmap b, int dstWidth, int dstHeight)    {        Bitmap dstImage = new Bitmap(dstWidth, dstHeight);        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dstImage);        //   设置插值模式         g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;        //   设置平滑模式         g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;        g.SmoothingMode = SmoothingMode.HighQuality;        g.InterpolationMode = InterpolationMode.HighQualityBicubic;        //用Graphic的DrawImage方法通过设置大小绘制新的图片实现缩放        g.DrawImage(b, new Rectangle(0, 0, dstImage.Width, dstImage.Height), new Rectangle(0, 0, b.Width, b.Height),GraphicsUnit.Pixel);        g.Save();        g.Dispose();        return dstImage;    }}

      然后就是调用这个类的方法实现图片处理,处理完的图片自己可以指定目录和名称。

下面就是方法调用:

            Image tempImg = Image.FromFile("D:\\fd9c3170bd0644049ae3e841744f964f_fbb525f8ec91b8f541311a512443efd3.jpg");            tempImg = ImageOperation.CreateRoundedCorner(tempImg, ImageOperation.RoundRectanglePosition.TopLeft);            tempImg = ImageOperation.CreateRoundedCorner(tempImg, ImageOperation.RoundRectanglePosition.TopRight);            tempImg = ImageOperation.CreateRoundedCorner(tempImg, ImageOperation.RoundRectanglePosition.BottomLeft);            tempImg = ImageOperation.CreateRoundedCorner(tempImg, ImageOperation.RoundRectanglePosition.BottomRight);            Bitmap completeimg = ImageOperation.ResizeBitmap(new Bitmap(tempImg), 80, 80);            completeimg.Save("D:\\new5.jpg");            pictureBox1.Image = completeimg;

           这是原图:

          这是处理后得到的80*80图片效果:

 

          圆角效果的程度在

//构建圆角外部路径 GraphicsPath rectPath = CreateRoundRectanglePath(rect, image.Width / 10, roundCorner); 中设置image.Width / 10大小就可以了。

 

原创粉丝点击