图像的合并及透明处理

            在实现图像的透明效果过程中,我们需要用到了ColorMatrix和ImageAttributes等类.
            而这些类包含在System.Drawing.Imaging名字空间中,所以我们在源代码文件的开始处需添加:
            using System.Drawing.Imaging;来实现对这些类的调用。
            还有,ImageAttributes类是用来设置图像的一系列属性的,它被用作Graphics类对象的DrawImage方法的一个参数。
            而ImageAttributes类对象的方法SetColorMatrix则调用ColorMatrix来设置图像的颜色值。
            而图像的透明效果正是ColorMatrix中部分值所决定的。

示例代码:

代码

        
/// <summary>
        
///  //原样绘制图像
        
/// </summary>
        private void ImageMerge()
        {
            Image myImage 
= pictureBox1.Image;
            
//创建画布, 一个Graphics实例,让它通过windows系统去与外部设备打交道;
            Graphics g = Graphics.FromImage(myImage);
            Image myMiniImage 
= Image.FromFile("C:\\TEST.PNG");  //要绘制的小的透明的图像
            
//g.DrawImage(myImage, 0, 0, 215, 340);
            g.DrawImage(myMiniImage, 90100, myMiniImage.Width, myMiniImage.Height);
            
this.pictureBox1.Image = myImage;
            g.Dispose();

        }




        
/// <summary>
        
/// //半透明或全透明处理
        
/// </summary>
        private void ImageMerge_Transparence()
        {




            Image myImage 
= pictureBox1.Image;
            
//创建画布, 一个Graphics实例,让它通过windows系统去与外部设备打交道;
            Graphics g = Graphics.FromImage(myImage);
            Image myMiniImage 
= Image.FromFile("C:\\TEST.PNG"); //要绘制的小的透明的图像
            float[][] ptsArray =
                     
new float[] {10000},
                     
new float[] {01000},
                     
new float[] {00100},
                     
new float[] {0000.5f0}, //注意:此处为0.5f,图像为半透明;此处为0.1f,图像为强透明;
                     new float[] {00001}};
            ColorMatrix clrMatrix 
= new ColorMatrix(ptsArray);
            ImageAttributes imgAttributes 
= new ImageAttributes();
            
//设置图像的颜色属性
            imgAttributes.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default,
            ColorAdjustType.Bitmap);
            
//画图像
            g.DrawImage(myMiniImage, new Rectangle(00, myMiniImage.Width, myMiniImage.Height),
                
00, myMiniImage.Width, myMiniImage.Height,
                GraphicsUnit.Pixel, imgAttributes);
            
this.pictureBox1.Image = myImage;
            g.Dispose();



        }

将颜色以字符串的形式保存与还原(便于存储到数据库中) 

代码
    if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                
this.pictureBox1.BackColor = colorDialog1.Color;

                
this.pictureBox2.BackColor = Color.FromArgb(100, colorDialog1.Color);
                
//参数 alpha: 新 Color 的 alpha 值。有效值为从 0 到 255。255完全不透明。
                
//以字符串形式保存当前颜色信息
                string HTMLCOLOR = System.Drawing.ColorTranslator.ToHtml(this.pictureBox1.BackColor);
                
//还原颜色信息
                this.button1.BackColor = System.Drawing.ColorTranslator.FromHtml(HTMLCOLOR);
            }

 

原文地址:https://www.cnblogs.com/furenjun/p/1624982.html