天天看點

C#圖檔處理類(顔色透明化,圖檔切割,圖檔合并,圖檔旋轉等)(轉)

                          目錄

1.背景透明化

2.指定顔色透明化

3.指定顔色替換成另一種顔色

4.圖檔按比例縮放

5.圖檔旋轉

6.圖檔更改透明度

7.圖檔添加文字

8.圖檔添加小圖

9.橫向合并兩張圖檔

10.縱向合并兩張圖檔

11.圖檔切割

以下的代碼都經過測試通過了的

1.

背景透明化

/// <summary>
        /// 背景透明化
        /// </summary>
        /// <param name="img">原圖</param>
        /// <param name="w">圖檔寬度</param>
        /// <param name="h">圖檔高度</param>
        /// <returns></returns>
        public static Bitmap Conver_1(Bitmap img, int w, int h)
        {
            Bitmap bg = new Bitmap(w, h);
            int alpha = 0;
            Color demo;
            Color pixel;
            for (int x = 0; x < w; x++)
            {
                for (int y = 0; y < h; y++)
                {
                    demo = img.GetPixel(1, 1);
                    pixel = img.GetPixel(x, y);
                    int R = demo.R;
                    int G = demo.G;
                    int B = demo.B;
                    int r1 = pixel.R;
                    int g1 = pixel.G;
                    int b1 = pixel.B;
                    int a = 40;  //RGB誤差範圍
                    if (Math.Abs(R - r1) < a && Math.Abs(G - g1) < a && Math.Abs(B - b1) < a)
                    {
                        alpha = 0;  //RGB在色差範圍内,透明度為0
                    }
                    else
                    {
                        alpha = 255;
                    }
                    bg.SetPixel(x, y, Color.FromArgb(alpha, r1, g1, b1));
                }
            }
            return bg;
        }
           

2.指定顔色透明化

/// <summary>
        /// 指定顔色透明化
        /// </summary>
        /// <param name="img">原圖</param>
        /// <param name="w">原圖寬度</param>
        /// <param name="h">原圖高度</param>
        /// <param name="R">指定顔色RGB的R值</param>
        /// <param name="G">指定顔色RGB的G值</param>
        /// <param name="B">指定顔色RGB的B值</param>
        /// <returns></returns>
        public static Bitmap Conver_2(Bitmap img, int w, int h, int R, int G, int B)
        {
            Bitmap bg = new Bitmap(w, h);
            int alpha = 0;
            Color pixel;
            for (int x = 0; x < w; x++)
            {
                for (int y = 0; y < h; y++)
                {
                    pixel = img.GetPixel(x, y);
                    int r1 = pixel.R;
                    int g1 = pixel.G;
                    int b1 = pixel.B;
                    int a = 40;  //色差範圍值
                    if (Math.Abs(R - r1) < a && Math.Abs(G - g1) < a && Math.Abs(B - b1) < a)
                    {
                        alpha = 0;    //若兩種顔色比較接近,透明度設為0
                    }
                    else
                    {
                        alpha = 255;
                    }
                    bg.SetPixel(x, y, Color.FromArgb(alpha, r1, g1, b1));
                }
            }
            return bg;
        }
           

3.指定顔色替換成另一種顔色

/// <summary>
        /// 指定顔色替換成另一種顔色
        /// </summary>
        /// <param name="img">原圖</param>
        /// <param name="w">圖寬</param>
        /// <param name="h">圖高</param>
        /// <param name="R">要被替換顔色的RGB的R</param>
        /// <param name="G">要被替換顔色的RGB的G</param>
        /// <param name="B">要被替換顔色的RGB的B</param>
        /// <param name="r">替換色的RGB的R</param>
        /// <param name="g">替換色的RGB的G</param>
        /// <param name="b">替換色的RGB的B</param>
        /// <returns></returns>
        public static Bitmap Conver_3(Bitmap img, int w, int h, int R, int G, int B, int r, int g, int b)
        {
            Bitmap bg = new Bitmap(w, h);
            Color pixel;
            for (int x = 0; x < w; x++)
            {
                for (int y = 0; y < h; y++)
                {
                    pixel = img.GetPixel(x, y);
                    int r1 = pixel.R;
                    int g1 = pixel.G;
                    int b1 = pixel.B;
                    int a = 40;
                    if (Math.Abs(R - r1) < a && Math.Abs(G - g1) < a && Math.Abs(B - b1) < a)
                    {
                        bg.SetPixel(x, y, Color.FromArgb(r, g, b));
                    }
                    else
                    {
                        bg.SetPixel(x, y, Color.FromArgb(r1, g1, b1));
                    }
 
                }
            }
            return bg;
        }
           

4.圖檔按比例縮放

/// <summary>
        /// 圖檔按比例縮放
        /// </summary>
        /// <param name="SourceImage">目标圖檔</param>
        /// <param name="a">縮放的比例</param>
        /// <returns></returns>
        public static Image PictureHandle1(Image SourceImage, float a)
        {
            int W;  //縮放後圖檔的寬
            int H;  //縮放後圖檔的高
            W = (int)(a * SourceImage.Width);
            H = (int)(a * SourceImage.Height);
            try
            {
                ImageFormat format = SourceImage.RawFormat;
                Bitmap SaveImage = new Bitmap(W, H);
                Graphics g = Graphics.FromImage(SaveImage);
                g.Clear(Color.White);
                g.DrawImage(SourceImage, 0, 0, W, H);
                SourceImage.Dispose();
                return SaveImage;
            }
            catch (Exception e)
            {
 
            }
            return null;
        }
           

5.圖檔旋轉

/// <summary>
        /// 圖檔旋轉
        /// </summary>
        /// <param name="b">原圖</param>
        /// <param name="savePath">旋轉後儲存路徑</param>
        /// <param name="angle">旋轉角度</param>
        public static void RotateImage(Image b, string savePath, float angle)
        {
            angle = angle % 360;            //弧度轉換
            double radian = angle * Math.PI / 180.0;
            double cos = Math.Cos(radian);
            double sin = Math.Sin(radian);
            //原圖的寬和高
            int w = b.Width;
            int h = b.Height;
            int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));
            int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));
            //目标位圖
            Image dsImage = new Bitmap(W, H);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dsImage);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //計算偏移量
            Point Offset = new Point((W - w) / 2, (H - h) / 2);
            //構造圖像顯示區域:讓圖像的中心與視窗的中心點一緻
            Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);
            Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
            g.TranslateTransform(center.X, center.Y);
            g.RotateTransform(360 - angle);
            //恢複圖像在水準和垂直方向的平移
            g.TranslateTransform(-center.X, -center.Y);
            g.DrawImage(b, rect);
            //重至繪圖的所有變換
            g.ResetTransform();
            g.Save();
            g.Dispose();
            string extension = Path.GetExtension(savePath);
            if (extension == ".jpg")
            {
                MessageBox.Show("圖檔儲存不是png格式,旋轉效果有誤!請重新選擇儲存格式!");
            }
            else
            {
                dsImage.Save(savePath, ImageFormat.Png);
                MessageBox.Show("圖檔旋轉成功!");
            }
        }
           

6.圖檔更改透明度

/// <summary>
        /// 圖檔更改透明度
        /// </summary>
        /// <param name="waterPic">水印圖</param>
        /// <param name="alpha">透明度值 0—255</param>
        /// <returns></returns>
        public static Image ChangeAlpha(Image waterPic, int alpha)
        {
            Bitmap img = new Bitmap(waterPic);
            using (Bitmap bmp = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
            {
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    g.DrawImage(img, 0, 0);
                    for (int h = 0; h < img.Height; h++)
                    {
                        for (int w = 0; w < img.Width; w++)
                        {
                            Color color = img.GetPixel(w, h);
                            bmp.SetPixel(w, h, Color.FromArgb(alpha, color.R, color.G, color.B));
                        }
                    }
                    return (Image)bmp.Clone();
                }
            }
        }
           

7.圖檔添加文字

/// <summary>
        /// 圖檔添加文字
        /// </summary>
        /// <param name="template">模闆路徑</param>
        /// <param name="savePath">儲存路徑</param>
        /// <param name="text">文字内容</param>
        /// <param name="font">字型</param>
        /// <param name="color">字型顔色</param>
        /// <param name="x">文字添加位置的起始坐标X</param>
        /// <param name="y">文字添加位置的起始坐标Y</param>
        public static void templateAddText(string template, string savePath, string text, Font font, Color color, int x, int y)
        {
            Image image = Image.FromFile(template);
            Bitmap bt = new Bitmap(image, image.Width, image.Height);
            Graphics g = Graphics.FromImage(bt);
            Font f = font;
            Brush b = new SolidBrush(color);
            g.DrawString(text, f, b, x, y);
            string extension = Path.GetExtension(savePath);
            if (extension == ".jpg")
            {
                bt.Save(savePath, ImageFormat.Jpeg);
            }
            else
            {
                bt.Save(savePath, ImageFormat.Png);
            }
        }
           

8.圖檔添加小圖

/// <summary>
        /// 圖檔添加小圖
        /// </summary>
        /// <param name="template">模闆圖路徑</param>
        /// <param name="pic">小圖路徑</param>
        /// <param name="savePath">儲存路徑</param>
        /// <param name="x">小圖添加位置起始坐标X</param>
        /// <param name="y">小圖添加位置起始坐标Y</param>
        public static void TemplateAddPicture(string template, string pic, string savePath, int x, int y)
        {
            Image image = Image.FromFile(template);
            Bitmap bt = new Bitmap(image, image.Width, image.Height);
            Image picture = Image.FromFile(pic);
            Bitmap small = new Bitmap(picture, picture.Width, picture.Height);
            Graphics g = Graphics.FromImage(bt);
            g.DrawImage(small, x, y, picture.Width, picture.Height);
            string extension = Path.GetExtension(savePath);
            if (extension == ".jpg")
            {
                bt.Save(savePath, ImageFormat.Jpeg);
            }
            else
            {
                bt.Save(savePath, ImageFormat.Png);
            }
 
        }
           

9.橫向合并兩張圖檔

/// <summary>
        /// 橫向合并兩張圖檔
        /// </summary>
        /// <param name="pic1">左圖檔案的路徑</param>
        /// <param name="pic2">右圖的檔案路徑</param>
        /// <param name="savePath">合成圖檔儲存路徑</param>
        public static void H_merge(string pic1, string pic2, string savePath)
        {
            Image img1 = Image.FromFile(pic1);
            Image img2 = Image.FromFile(pic2);
            Bitmap L = new Bitmap(img1, img1.Width, img1.Height);
            Bitmap R = new Bitmap(img2, img2.Width, img2.Height);
            if (img2.Height == img1.Height)
            {
                Bitmap bg = new Bitmap(img1.Width + img2.Width, img1.Height);
                Graphics g = Graphics.FromImage(bg);
                g.DrawImage(L, 0, 0, img1.Width, img1.Height);
                g.DrawImage(R, img1.Width, 0, img2.Width, img2.Height);
                g.Dispose();
                string extension = Path.GetExtension(savePath);
                if (extension == ".jpg")
                {
                    bg.Save(savePath, ImageFormat.Jpeg);
                }
                else
                {
                    bg.Save(savePath, ImageFormat.Png);
                }
                MessageBox.Show("圖檔合成成功!");
            }
            else
            {
                MessageBox.Show("兩張圖檔的高度不一緻,請重新選擇!");
            }
        }
           

10.縱向合并兩張圖檔

/// <summary>
        /// 縱向合并兩張圖檔
        /// </summary>
        /// <param name="pic1">上圖檔案的路徑</param>
        /// <param name="pic2">下圖的檔案路徑</param>
        /// <param name="savePath">合成圖檔儲存路徑</param>
        public static void Z_merge(string pic1, string pic2, string savePath)
        {
            Image img1 = Image.FromFile(pic1);
            Image img2 = Image.FromFile(pic2);
            Bitmap L = new Bitmap(img1, img1.Width, img1.Height);
            Bitmap R = new Bitmap(img2, img2.Width, img2.Height);
            if (img2.Width == img1.Width)
            {
                Bitmap bg = new Bitmap(img1.Width, img1.Height + img2.Height);
                Graphics g = Graphics.FromImage(bg);
                g.DrawImage(L, 0, 0, img1.Width, img1.Height);
                g.DrawImage(R, 0, img1.Height, img2.Width, img2.Height);
                g.Dispose();
                string extension = Path.GetExtension(savePath);
                if (extension == ".jpg")
                {
                    bg.Save(savePath, ImageFormat.Jpeg);
                }
                else
                {
                    bg.Save(savePath, ImageFormat.Png);
                }
                MessageBox.Show("圖檔合成成功!");
            }
            else
            {
                MessageBox.Show("兩張圖檔的寬度不一緻,請重新選擇!");
            }
        }
           

11.圖檔切割

/// <summary>
        /// 圖檔切割
        /// </summary>
        /// <param name="template">原圖路徑</param>
        /// <param name="savePath">切割圖儲存路徑</param>
        /// <param name="x">切割起始位置的坐标X</param>
        /// <param name="y">切割起始位置的坐标Y</param>
        /// <param name="w">切割寬度</param>
        /// <param name="h">切割高度</param>
        public static void pic_cut(string template, string savePath, int x, int y, int w, int h)
        {
            Image image = Image.FromFile(template);
            Bitmap b = new Bitmap(image, image.Width, image.Height);
            Bitmap bmpOut = new Bitmap(w, h, PixelFormat.Format24bppRgb);
            Graphics g = Graphics.FromImage(bmpOut);
            g.DrawImage(b, new Rectangle(0, 0, w, h), new Rectangle(x, y, w, h), GraphicsUnit.Pixel);
            g.Dispose();
            string extension = Path.GetExtension(savePath);
            if (extension == ".jpg")
            {
                bmpOut.Save(savePath, ImageFormat.Jpeg);
            }
            else
            {
                bmpOut.Save(savePath, ImageFormat.Png);
            }
        }
           

背景透明的png格式的圖檔旋轉後作為水印貼圖檔上有黑色效果,有風險

繼續閱讀