图像的合并及透明处理

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

 

示例代码:

ExpandedBlockStart.gif 代码

        
///   <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,  90 100 , 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 [] { 1 0 0 0 0 },
                     
new   float [] { 0 1 0 0 0 },
                     
new   float [] { 0 0 1 0 0 },
                     
new   float [] { 0 0 0 0.5f 0 },  // 注意:此处为0.5f,图像为半透明;此处为0.1f,图像为强透明;
                      new   float [] { 0 0 0 0 1 }};
            ColorMatrix clrMatrix 
=   new  ColorMatrix(ptsArray);
            ImageAttributes imgAttributes 
=   new  ImageAttributes();
            
// 设置图像的颜色属性
            imgAttributes.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default,
            ColorAdjustType.Bitmap);
            
// 画图像
            g.DrawImage(myMiniImage,  new  Rectangle( 0 0 , myMiniImage.Width, myMiniImage.Height),
                
0 0 , myMiniImage.Width, myMiniImage.Height,
                GraphicsUnit.Pixel, imgAttributes);
            
this .pictureBox1.Image  =  myImage;
            g.Dispose();



        }

 

 

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

 

ExpandedBlockStart.gif 代码
     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);
            }

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值