图像处理类

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Diagnostics;
using System.Drawing.Imaging;

namespace Lhyi.Helper
{
    /// <summary>
    /// 图像处理类
    /// </summary>
    public class ImageProcess
    {
        #region 方法
        
        /// <summary>
        /// 图片无损缩放
        /// </summary>
        /// <param name="source">源图片</param>
        /// <param name="destHeight">缩放后图片高度</param>
        /// <param name="destWidth">缩放后图片宽度</param>
        /// <returns></returns>
        public static Bitmap GetThumbnail(Image source, int destWidth, int destHeight)
        {
            Bitmap bitmap = null;
            try
            {
                System.Drawing.Imaging.ImageFormat sourceFormat = source.RawFormat;
                int sW = 0, sH = 0;
                // 按比例缩放
                int sWidth = source.Width;
                int sHeight = source.Height;

                if (sHeight > destHeight || sWidth > destWidth)
                {
                    if ((sWidth * destHeight) > (sHeight * destWidth))
                    {
                        sW = destWidth;
                        sH = (destWidth * sHeight) / sWidth;
                    }
                    else
                    {
                        sH = destHeight;
                        sW = (sWidth * destHeight) / sHeight;
                    }
                }
                else
                {
                    sW = sWidth;
                    sH = sHeight;
                }

                bitmap = new Bitmap(destWidth, destHeight);
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.Clear(Color.WhiteSmoke);

                    // 设置画布的描绘质量
                    g.CompositingQuality = CompositingQuality.HighQuality;
                    g.SmoothingMode = SmoothingMode.HighQuality;
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                    Rectangle rect = new Rectangle((destWidth - sW) / 2, (destHeight - sH) / 2, sW, sH);
                    g.DrawImage(source, rect, 0, 0, source.Width, source.Height, GraphicsUnit.Pixel);

                    source.Dispose();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("ImageProcess.GEtThumbnail(Image, int, int) :: " + ex.Message);
                throw ex;
            }
            return bitmap;
        }

        /// <summary>
        /// 图片无损缩放
        /// </summary>
        /// <param name="filePath">图片源路径</param>
        /// <param name="destHeight">缩放后图片高度</param>
        /// <param name="destWidth">缩放后图片宽度</param>
        /// <returns></returns>
        public static Bitmap GetThumbnail(string filePath, int destWidth, int destHeight)
        {
            Bitmap bitmap = null;
            try
            {
                using (Image source = Image.FromFile(filePath))
                {
                    ImageProcess.GetThumbnail(source, destWidth, destHeight);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("ImageProcess.GetThumbnail(string, int, int) :: " + ex.Message);
                throw ex;
            }
            return bitmap;
        }

        /// <summary>
        /// 截取屏幕图像
        /// </summary>
        /// <returns></returns>
        public static Bitmap GetScreenPic()
        {
            Bitmap bitmap = null;
            try
            {
                Size size = Screen.PrimaryScreen.WorkingArea.Size;
                if (bitmap == null)
                    bitmap = new Bitmap(size.Width, size.Height);
                Graphics g = Graphics.FromImage(bitmap);
                g.CopyFromScreen(Point.Empty, Point.Empty, size);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("ImageProcess.GetScreenPic() :: " + ex.Message);
                throw ex;
            }
            return bitmap;
        }

        /// <summary>
        /// 灰度
        /// </summary>
        /// <param name="image">需要处理成灰度的图片对象</param>
        /// <returns></returns>
        public static Bitmap ToGray(Image image)
        {
            Bitmap bitmap = new Bitmap(image);
            try
            {
                image.Dispose();
                image = null;

                int width = bitmap.Width;
                int height = bitmap.Height;
                Rectangle rect = new Rectangle(0, 0, width, height);
                //用可读写的方式锁定全部位图像素
                BitmapData bmpData = bitmap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                byte gray = 0;
                unsafe//启用不安全模式
                {
                    byte* p = (byte*)bmpData.Scan0;//获取首地址
                    int offset = bmpData.Stride - width * 3;
                    //二维图像循环
                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            //gray = (byte)((float)p[0] * 0.114f + (float)p[1] * 0.587f + (float)p[2] * 0.299f);
                            gray = (byte)((float)(p[0] + p[1] + p[2]) / 3.0f);
                            p[2] = p[1] = p[0] = (byte)gray;
                            p += 3;
                        }
                        p += offset;
                    }
                }
                bitmap.UnlockBits(bmpData);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("ImageProcess.ToGray(Image) :: " + ex.Message);
                throw ex;
            }
            #region 效果不如前者

            得到首地址
            //IntPtr ptr = bmpData.Scan0;
            24位bmp位图字节数
            //int bytes = width * height * 3;
            //byte[] rgbValues = new byte[bytes];
            //Marshal.Copy(ptr, rgbValues, 0, bytes);
            灰度化
            //double colorTemp = 0;
            //for (int i = 0; i < bytes; i += 3)
            //{
            //    //colorTemp = (byte)(rgbValues[i] * 0.114f + rgbValues[i + 1] * 0.587f + rgbValues[i + 2] * 0.299f);
            //    colorTemp = (rgbValues[i] + rgbValues[i + 1] + rgbValues[i + 2]) / 3;
            //    rgbValues[i] = rgbValues[i + 1] = rgbValues[i + 2] = (byte)colorTemp;
            //}
            还原位图
            //Marshal.Copy(rgbValues, 0, ptr, bytes);
            //bitmap.UnlockBits(bmpData);
            #endregion

            return bitmap;
        }

        #endregion
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值