怎么利用C#创建透明的GIF图片

Code
using System; 
using System.IO; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data; 
  
namespace TransparentGifCreator 

  
/**//// <summary> 
  
/// Summary description for Form1. 
  
/// </summary> 

  public class Form1 : System.Windows.Forms.Form 
  

private System.Windows.Forms.PictureBox pictureBox1; 
private System.Windows.Forms.Panel panel1; 
private System.Windows.Forms.Button button1; 
private System.Windows.Forms.Button button2; 
private System.Windows.Forms.Button button3; 
private System.ComponentModel.IContainer components; 
  
Image _gifImage; 
private System.Windows.Forms.Timer timer1; 
ColorPalette cp; 
int CurrentEntry; 
  
public Form1() 

// 
// Required for Windows Form Designer support 
// 
InitializeComponent(); 
  
// 
// TODO: Add any constructor code after InitializeComponent call 
// 
}
 
  
/**//// <summary> 
/// Clean up any resources being used. 
/// </summary> 

protected override void Dispose( bool disposing ) 

if( disposing ) 

if (components != null

components.Dispose(); 
}
 
}
 
base.Dispose( disposing ); 
}
 
  
Windows Form Designer generated code#region Windows Form Designer generated code 
/**//// <summary> 
/// Required method for Designer support - do not modify 
/// the contents of this method with the code editor. 
/// </summary> 

private void InitializeComponent() 

this.components = new System.ComponentModel.Container(); 
this.pictureBox1 = new System.Windows.Forms.PictureBox(); 
this.panel1 = new System.Windows.Forms.Panel(); 
this.button1 = new System.Windows.Forms.Button(); 
this.button2 = new System.Windows.Forms.Button(); 
this.button3 = new System.Windows.Forms.Button(); 
this.timer1 = new System.Windows.Forms.Timer(this.components); 
this.SuspendLayout(); 
// 
// pictureBox1 
// 
this.pictureBox1.Location = new System.Drawing.Point(968); 
this.pictureBox1.Name = "pictureBox1"
this.pictureBox1.Size = new System.Drawing.Size(144144); 
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; 
this.pictureBox1.TabIndex = 0
this.pictureBox1.TabStop = false
// 
// panel1 
// 
this.panel1.Location = new System.Drawing.Point(8168); 
this.panel1.Name = "panel1"
this.panel1.Size = new System.Drawing.Size(144144); 
this.panel1.TabIndex = 1
this.panel1.Click += new System.EventHandler(this.panel1_Click); 
this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint); 
this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseMove); 
// 
// button1 
// 
this.button1.Location = new System.Drawing.Point(200176); 
this.button1.Name = "button1"
this.button1.Size = new System.Drawing.Size(8824); 
this.button1.TabIndex = 2
this.button1.Text = "Open"
this.button1.Click += new System.EventHandler(this.button1_Click); 
// 
// button2 
// 
this.button2.Location = new System.Drawing.Point(200216); 
this.button2.Name = "button2"
this.button2.Size = new System.Drawing.Size(8824); 
this.button2.TabIndex = 2
this.button2.Text = "Save"
this.button2.Click += new System.EventHandler(this.button2_Click); 
// 
// button3 
// 
this.button3.Location = new System.Drawing.Point(200256); 
this.button3.Name = "button3"
this.button3.Size = new System.Drawing.Size(8824); 
this.button3.TabIndex = 2
this.button3.Text = "Exit"
this.button3.Click += new System.EventHandler(this.button3_Click); 
// 
// timer1 
// 
this.timer1.Enabled = true
this.timer1.Interval = 250
this.timer1.Tick += new System.EventHandler(this.timer1_Tick); 
// 
// Form1 
// 
this.AutoScaleBaseSize = new System.Drawing.Size(513); 
this.ClientSize = new System.Drawing.Size(328325); 
this.Controls.Add(this.button1); 
this.Controls.Add(this.panel1); 
this.Controls.Add(this.pictureBox1); 
this.Controls.Add(this.button2); 
this.Controls.Add(this.button3); 
this.Name = "Form1"
this.Text = "Form1"
this.ResumeLayout(false); 
  
}
 
#endregion
 
  
/**//// <summary> 
/// The main entry point for the application. 
/// </summary> 

[STAThread] 
static void Main() 

Application.Run(
new Form1()); 
}
 
  
private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) 

if(cp==null
return
  
for(float y=0;y<16;y++
for(float x=0;x<16;x++

Color c
=Color.Black; 
if( ((16*y) + x)<cp.Entries.Length) 
c
=cp.Entries[(int)((16*y)+x)]; 
SolidBrush sb
=new SolidBrush(Color.FromArgb(255,c)); 
float w=((float)this.panel1.Width)/16
float h=((float)this.panel1.Height)/16
e.Graphics.FillRectangle(sb,w
*x,h*y,w,h); 
if(c.A!=255

if(showTrans) 
e.Graphics.DrawRectangle(Pens.Black,w
*x,h*y,w-1,h-1); 
else 
e.Graphics.DrawRectangle(Pens.White,w
*x,h*y,w-1,h-1); 
}
 
  
sb.Dispose(); 
}
 
}
 
  
  
private void panel1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) 

int y=(int)(((float)e.Y)/(((float)this.panel1.Width)/16f)); 
int x=(int)(((float)e.X)/(((float)this.panel1.Height)/16f)); 
CurrentEntry
=(int)((16*y)+x); 
if(cp!=null

if(CurrentEntry>=cp.Entries.Length) 
CurrentEntry
=cp.Entries.Length-1
//Little bit of diagnostic for the palette chooser below 
//System.Diagnostics.Trace.WriteLine(string.Format("{0},{1}, adjusted={4},{5} entry={2} Colour={3}",e.X,e.Y,CurrentEntry,cp.Entries[CurrentEntry].ToString(),x,y)); 
}
 
}
 
  
private void panel1_Click(object sender, System.EventArgs e) 

//Creates a new GIF image with a modified colour palette 
if(cp!=null

//Create a new 8 bit per pixel image 
Bitmap bm=new Bitmap(_gifImage.Width,_gifImage.Height,PixelFormat.Format8bppIndexed); 
//get it's palette 
ColorPalette ncp=bm.Palette; 
  
//copy all the entries from the old palette removing any transparency 
int n=0
foreach(Color c in cp.Entries) 
ncp.Entries[n
++]=Color.FromArgb(255,c); 
  
//Set the newly selected transparency 
ncp.Entries[CurrentEntry]=Color.FromArgb(0,cp.Entries[CurrentEntry]); 
//re-insert the palette 
bm.Palette=ncp; 
  
//now to copy the actual bitmap data 
//lock the source and destination bits 
BitmapData src=((Bitmap)_gifImage).LockBits(new Rectangle(0,0,_gifImage.Width,_gifImage.Height),ImageLockMode.ReadOnly,_gifImage.PixelFormat); 
BitmapData dst
=bm.LockBits(new Rectangle(0,0,bm.Width,bm.Height),ImageLockMode.WriteOnly,bm.PixelFormat); 
  
//uses pointers so we need unsafe code. 
//the project is also compiled with /unsafe 
unsafe 

//steps through each pixel 
for(int y=0;y<_gifImage.Height;y++
for(int x=0;x<_gifImage.Width;x++

//transferring the bytes 
((byte *)dst.Scan0.ToPointer())[(dst.Stride*y)+x]=((byte *)src.Scan0.ToPointer())[(src.Stride*y)+x]; 
}
 
}
 
  
//all done, unlock the bitmaps 
((Bitmap)_gifImage).UnlockBits(src); 
bm.UnlockBits(dst); 
  
//clear out the picturebox 
this.pictureBox1.Image=null
_gifImage.Dispose(); 
//set the new image in place 
_gifImage=bm; 
cp
=_gifImage.Palette; 
this.pictureBox1.Image=_gifImage; 
}
 
}
 
  
private void button1_Click(object sender, System.EventArgs e) 

OpenFileDialog dlg
=new OpenFileDialog(); 
dlg.Filter
="GIF files|*.GIF"
if(dlg.ShowDialog()==DialogResult.OK) 

_gifImage
=Image.FromFile(dlg.FileName); 
this.pictureBox1.Image=_gifImage; 
cp
=_gifImage.Palette; 
this.panel1.Invalidate(); 
}
 
}
 
  
private void button2_Click(object sender, System.EventArgs e) 

SaveFileDialog dlg
=new SaveFileDialog(); 
dlg.Filter
="GIF files|*.gif"
dlg.DefaultExt
=".gif"
dlg.AddExtension
=true
if(dlg.ShowDialog()==DialogResult.OK) 

_gifImage.Save(dlg.FileName,ImageFormat.Gif); 
}
 
}
 
  
private void button3_Click(object sender, System.EventArgs e) 

Application.Exit(); 
}
 
  
bool showTrans; 
  
private void timer1_Tick(object sender, System.EventArgs e) 

showTrans
^=true
Graphics g
=this.panel1.CreateGraphics(); 
//I do this rather than invalidate the panel because 
//the panel draws its background ans so flickers horribly. 
PaintEventArgs pe=new PaintEventArgs(g,new Rectangle(0,0,this.panel1.Width,this.panel1.Height)); 
this.panel1_Paint(this,pe); 
g.Dispose(); 
}
 
  }
 
}
 
 
-----------------------------------

using System; 

using System.IO; 

using System.Drawing; 

using System.Drawing.Imaging; 

using System.Collections; 

using System.ComponentModel; 

using System.Windows.Forms; 

using System.Data; 

  

namespace TransparentGifCreator 



  
/**//// <summary> 

  
/// Summary description for Form1. 

  
/// </summary> 


  
public class Form1 : System.Windows.Forms.Form 

  


    
private System.Windows.Forms.PictureBox pictureBox1; 

    
private System.Windows.Forms.Panel panel1; 

    
private System.Windows.Forms.Button button1; 

    
private System.Windows.Forms.Button button2; 

    
private System.Windows.Forms.Button button3; 

    
private System.ComponentModel.IContainer components; 

  

    Image _gifImage; 

    
private System.Windows.Forms.Timer timer1; 

    ColorPalette cp; 

    
int CurrentEntry; 

  

    
public Form1() 

    


      
// 

      
// Required for Windows Form Designer support 

      
// 

      InitializeComponent(); 

  

      
// 

      
// TODO: Add any constructor code after InitializeComponent call 

      
// 

    }
 

  

    
/**//// <summary> 

    
/// Clean up any resources being used. 

    
/// </summary> 


    
protected override void Dispose( bool disposing ) 

    


      
if( disposing ) 

      


        
if (components != null

        


          components.Dispose(); 

        }
 

      }
 

      
base.Dispose( disposing ); 

    }
 

  

    
Windows Form Designer generated code#region Windows Form Designer generated code 

    
/**//// <summary> 

    
/// Required method for Designer support - do not modify 

    
/// the contents of this method with the code editor. 

    
/// </summary> 


    
private void InitializeComponent() 

    


      
this.components = new System.ComponentModel.Container(); 

      
this.pictureBox1 = new System.Windows.Forms.PictureBox(); 

      
this.panel1 = new System.Windows.Forms.Panel(); 

      
this.button1 = new System.Windows.Forms.Button(); 

      
this.button2 = new System.Windows.Forms.Button(); 

      
this.button3 = new System.Windows.Forms.Button(); 

      
this.timer1 = new System.Windows.Forms.Timer(this.components); 

      
this.SuspendLayout(); 

      
// 

      
// pictureBox1 

      
// 

      
this.pictureBox1.Location = new System.Drawing.Point(968); 

      
this.pictureBox1.Name = "pictureBox1"

      
this.pictureBox1.Size = new System.Drawing.Size(144144); 

      
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; 

      
this.pictureBox1.TabIndex = 0

      
this.pictureBox1.TabStop = false

      
// 

      
// panel1 

      
// 

      
this.panel1.Location = new System.Drawing.Point(8168); 

      
this.panel1.Name = "panel1"

      
this.panel1.Size = new System.Drawing.Size(144144); 

      
this.panel1.TabIndex = 1

      
this.panel1.Click += new System.EventHandler(this.panel1_Click); 

      
this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint); 

      
this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseMove); 

      
// 

      
// button1 

      
// 

      
this.button1.Location = new System.Drawing.Point(200176); 

      
this.button1.Name = "button1"

      
this.button1.Size = new System.Drawing.Size(8824); 

      
this.button1.TabIndex = 2

      
this.button1.Text = "Open"

      
this.button1.Click += new System.EventHandler(this.button1_Click); 

      
// 

      
// button2 

      
// 

      
this.button2.Location = new System.Drawing.Point(200216); 

      
this.button2.Name = "button2"

      
this.button2.Size = new System.Drawing.Size(8824); 

      
this.button2.TabIndex = 2

      
this.button2.Text = "Save"

      
this.button2.Click += new System.EventHandler(this.button2_Click); 

      
// 

      
// button3 

      
// 

      
this.button3.Location = new System.Drawing.Point(200256); 

      
this.button3.Name = "button3"

      
this.button3.Size = new System.Drawing.Size(8824); 

      
this.button3.TabIndex = 2

      
this.button3.Text = "Exit"

      
this.button3.Click += new System.EventHandler(this.button3_Click); 

      
// 

      
// timer1 

      
// 

      
this.timer1.Enabled = true

      
this.timer1.Interval = 250

      
this.timer1.Tick += new System.EventHandler(this.timer1_Tick); 

      
// 

      
// Form1 

      
// 

      
this.AutoScaleBaseSize = new System.Drawing.Size(513); 

      
this.ClientSize = new System.Drawing.Size(328325); 

      
this.Controls.Add(this.button1); 

      
this.Controls.Add(this.panel1); 

      
this.Controls.Add(this.pictureBox1); 

      
this.Controls.Add(this.button2); 

      
this.Controls.Add(this.button3); 

      
this.Name = "Form1"

      
this.Text = "Form1"

      
this.ResumeLayout(false); 

  

    }
 

    
#endregion
 

  

    
/**//// <summary> 

    
/// The main entry point for the application. 

    
/// </summary> 


    [STAThread] 

    
static void Main() 

    


      Application.Run(
new Form1()); 

    }
 

  

    
private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) 

    


      
if(cp==null

        
return

  

      
for(float y=0;y<16;y++

        
for(float x=0;x<16;x++

        


          Color c
=Color.Black; 

          
if( ((16*y) + x)<cp.Entries.Length) 

            c
=cp.Entries[(int)((16*y)+x)]; 

          SolidBrush sb
=new SolidBrush(Color.FromArgb(255,c)); 

          
float w=((float)this.panel1.Width)/16

          
float h=((float)this.panel1.Height)/16

          e.Graphics.FillRectangle(sb,w
*x,h*y,w,h); 

          
if(c.A!=255

          


            
if(showTrans) 

              e.Graphics.DrawRectangle(Pens.Black,w
*x,h*y,w-1,h-1); 

            
else 

              e.Graphics.DrawRectangle(Pens.White,w
*x,h*y,w-1,h-1); 

          }
 

  

          sb.Dispose(); 

        }
 

    }
 

  

  

    
private void panel1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) 

    


      
int y=(int)(((float)e.Y)/(((float)this.panel1.Width)/16f)); 

      
int x=(int)(((float)e.X)/(((float)this.panel1.Height)/16f)); 

      CurrentEntry
=(int)((16*y)+x); 

      
if(cp!=null

      


        
if(CurrentEntry>=cp.Entries.Length) 

          CurrentEntry
=cp.Entries.Length-1

        
//Little bit of diagnostic for the palette chooser below 

        
//System.Diagnostics.Trace.WriteLine(string.Format("{0},{1}, adjusted={4},{5} entry={2} Colour={3}",e.X,e.Y,CurrentEntry,cp.Entries[CurrentEntry].ToString(),x,y)); 

      }
 

    }
 

  

    
private void panel1_Click(object sender, System.EventArgs e) 

    


      
//Creates a new GIF image with a modified colour palette 

      
if(cp!=null

      


        
//Create a new 8 bit per pixel image 

        Bitmap bm
=new Bitmap(_gifImage.Width,_gifImage.Height,PixelFormat.Format8bppIndexed); 

        
//get it's palette 

        ColorPalette ncp
=bm.Palette; 

  

        
//copy all the entries from the old palette removing any transparency 

        
int n=0

        
foreach(Color c in cp.Entries) 

          ncp.Entries[n
++]=Color.FromArgb(255,c); 

  

        
//Set the newly selected transparency 

        ncp.Entries[CurrentEntry]
=Color.FromArgb(0,cp.Entries[CurrentEntry]); 

        
//re-insert the palette 

        bm.Palette
=ncp; 

  

        
//now to copy the actual bitmap data 

        
//lock the source and destination bits 

        BitmapData src
=((Bitmap)_gifImage).LockBits(new Rectangle(0,0,_gifImage.Width,_gifImage.Height),ImageLockMode.ReadOnly,_gifImage.PixelFormat); 

        BitmapData dst
=bm.LockBits(new Rectangle(0,0,bm.Width,bm.Height),ImageLockMode.WriteOnly,bm.PixelFormat); 

  

        
//uses pointers so we need unsafe code. 

        
//the project is also compiled with /unsafe 

        
unsafe 

        


          
//steps through each pixel 

          
for(int y=0;y<_gifImage.Height;y++

            
for(int x=0;x<_gifImage.Width;x++

            


              
//transferring the bytes 

              ((
byte *)dst.Scan0.ToPointer())[(dst.Stride*y)+x]=((byte *)src.Scan0.ToPointer())[(src.Stride*y)+x]; 

            }
 

        }
 

  

        
//all done, unlock the bitmaps 

        ((Bitmap)_gifImage).UnlockBits(src); 

        bm.UnlockBits(dst); 

  

        
//clear out the picturebox 

        
this.pictureBox1.Image=null

        _gifImage.Dispose(); 

        
//set the new image in place 

        _gifImage
=bm; 

        cp
=_gifImage.Palette; 

        
this.pictureBox1.Image=_gifImage; 

      }
 

    }
 

  

    
private void button1_Click(object sender, System.EventArgs e) 

    


      OpenFileDialog dlg
=new OpenFileDialog(); 

      dlg.Filter
="GIF files|*.GIF"

      
if(dlg.ShowDialog()==DialogResult.OK) 

      


        _gifImage
=Image.FromFile(dlg.FileName); 

        
this.pictureBox1.Image=_gifImage; 

        cp
=_gifImage.Palette; 

        
this.panel1.Invalidate(); 

      }
 

    }
 

  

    
private void button2_Click(object sender, System.EventArgs e) 

    


      SaveFileDialog dlg
=new SaveFileDialog(); 

      dlg.Filter
="GIF files|*.gif"

      dlg.DefaultExt
=".gif"

      dlg.AddExtension
=true

      
if(dlg.ShowDialog()==DialogResult.OK) 

      


        _gifImage.Save(dlg.FileName,ImageFormat.Gif); 

      }
 

    }
 

  

    
private void button3_Click(object sender, System.EventArgs e) 

    


      Application.Exit(); 

    }
 

  

    
bool showTrans; 

  

    
private void timer1_Tick(object sender, System.EventArgs e) 

    


      showTrans
^=true

      Graphics g
=this.panel1.CreateGraphics(); 

      
//I do this rather than invalidate the panel because 

      
//the panel draws its background ans so flickers horribly. 

      PaintEventArgs pe
=new PaintEventArgs(g,new Rectangle(0,0,this.panel1.Width,this.panel1.Height)); 

      
this.panel1_Paint(this,pe); 

      g.Dispose(); 

    }
 

  }
 

}
 

转载于:https://www.cnblogs.com/zhy4606/archive/2008/01/25/1053203.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值