Image 颜色分量 、获取略缩图

来源:互联网 发布:当地金融数据平台 编辑:程序博客网 时间:2024/05/16 15:42

ColorMatrix

灰色,图片去色
r = 像素点的红色分量0.299 + 像素点的绿色分量0.587 + 像素点的蓝色分量0.114

 ColorMatrix _matrix = new ColorMatrix(new float[][]{
                new float[]{0.299f,0.299f,0.299f,0.299f,0.299f},
                new float[]{0.587f,0.587f,0.587f,0.587f,0.587f},
                new float[]{0.114f,0.114f,0.114f,0.114f,0.114f},
                new float[]{0,0,0,1,0},
                new float[]{0,0,0,0,1}

 ///运用

 ///sourimg 源图片,rectSize 保存图片大小

 private Image GetImage(string sourimg,Rectangle rectSize)
        {
            ColorMatrix colorMatr =  new ColorMatrix(new float[][]{
                new float[]{0.299f,0.299f,0.299f,0.299f,0.299f},
                new float[]{0.587f,0.587f,0.587f,0.587f,0.587f},
                new float[]{0.114f,0.114f,0.114f,0.114f,0.114f},
                new float[]{0,0,0,1,0},
                new float[]{0,0,0,0,1};

       

            Image imageDemo = Image.FromFile(sourimg, true);

            Bitmap bitmap = new Bitmap(rectSize.Width, rectSize.Height);
            Graphics g= Graphics.FromImage(bitmap);
            ImageAttributes imagAtt = new ImageAttributes();
            imagAtt.SetColorMatrix(colorMatr);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.SmoothingMode = SmoothingMode.HighSpeed;
            g.DrawImage(imageDemo, rectSize, 0, 0, imageDemo.Width, imageDemo.Height, GraphicsUnit.Pixel, imagAtt);

            return bitmap;
        }

------------------------------------------------------------分界线-----------------------------------------------

      // 产生缩略图
        private void Thumb(string imgPath, string thumbPath)
        {
            System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
            System.Drawing.Image img = System.Drawing.Image.FromFile(imgPath); // 通过文件构造
            //生成缩略图
       
            System.Drawing.Image myThumbnail = img.GetThumbnailImage(100, 50, myCallback, IntPtr.Zero);
            myThumbnail.Save(thumbPath); // 保存缩略图
        }
        // 取消操作回调
        private bool ThumbnailCallback()
        {
            return false;
        }

原创粉丝点击