關於我 | SQL Server 服務

MVP

MVP

 

C#(2010年第二季)

SQL Server(2011~2014第二季)

SQLPASSTW
twMVC MVA

free counters

 

最新回應

[C#][WinForm]如何合併圖片

看到網友發問,自己隨手記錄一下。

一般合併圖片大多情況有三種,水平、垂直、浮水印,這裡簡單實做一下。

 

來源兩張圖片

image image

水平合併

 private Image HorizontalMergeImages(Image img1, Image img2)
        {
            Image MergedImage = default(Image);
            Int32 Wide = 0;
            Int32 High = 0;          
            Wide = img1.Width + img2.Width;//設定寬度           
            if (img1.Height >= img2.Height)
            {
                High = img1.Height;
            }
            else
            {
                High = img2.Height;
            }  
            Bitmap mybmp = new Bitmap(Wide, High);
            Graphics gr = Graphics.FromImage(mybmp); 
            //處理第一張圖片
            gr.DrawImage(img1, 0, 0);           
            //處理第二張圖片
            gr.DrawImage(img2, img1.Width, 0);
            MergedImage = mybmp;
            gr.Dispose();           
            return MergedImage;
        }    

結果

image

 

垂直合併

 private Image HorizontalMergeImages(Image img1, Image img2)
        {
            Image MergedImage = default(Image);
            Int32 Wide = 0;
            Int32 High = 0;          
            High = img1.Height + img2.Height;//設定高度          
            if (img1.Width >= img2.Width)
            {
                Wide = img1.Width;
            }
            else
            {
                Wide = img2.Width;
            } 
            Bitmap mybmp = new Bitmap(Wide, High);
            Graphics gr = Graphics.FromImage(mybmp); 
            //處理第一張圖片
            gr.DrawImage(img1, 0, 0);           
            //處理第二張圖片
            gr.DrawImage(img2, 0, img1.Height);
            MergedImage = mybmp;
            gr.Dispose();           
            return MergedImage;
        }  

結果

image

 

圖片浮水印

private Image MarkImage(Image img1, Image img2)
        {          
            Image MergedImage = default(Image);
            //設定背景圖片
            Graphics gr = System.Drawing.Graphics.FromImage(img1); 
            //新建logo浮水印圖片
            Bitmap Logo = new Bitmap(img2.Width, img2.Height); 
            Graphics tgr = Graphics.FromImage(Logo);
            ColorMatrix cmatrix = new ColorMatrix();  
            //設定圖片色彩(透明度)
            cmatrix.Matrix33 = 0.5F;  
            ImageAttributes imgattributes = new ImageAttributes();
            imgattributes.SetColorMatrix(cmatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            tgr.DrawImage(img2, new Rectangle(0, 0, Logo.Width, Logo.Height), 0, 0, Logo.Width, Logo.Height, GraphicsUnit.Pixel, imgattributes);
            tgr.Dispose();
            //logo圖片位置
            gr.DrawImage(Logo, img1.Width/3,10);           
            gr.Dispose();
            MergedImage = img1;
            return MergedImage;
        }

結果

image


引用文章請註明出處,勿整篇內容複製,謝謝。


DotBlogs Tags: C# WinForm

關連文章

[C#][WinForm]UI別在無回應了

[C#][Tips]比較Int32.TryParse、Int32.Parse And Convert.ToInt32

[C#][WinForm]如何取得當前視窗

[C#][WebForm]Excel to GridView

回應

  • # re: [C#][WinForm]如何合併圖片 by pcdou

    我買了新索隆&喬巴~~哈哈哈~~
    (完全無意義的一篇回應...= =||)

    2009/12/21 下午 02:15 | 回覆

登入後使用進階評論

Please add 2 and 2 and type the answer here: