关闭

[johnsuna(阿山NET)的专栏]怎么利用C#创建透明的GIF图片?(可自定义调色板),收藏一下.

1644人阅读 评论(0) 收藏 举报
 

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 );
}
 
#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(96, 8);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(144, 144);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// panel1
//
this.panel1.Location = new System.Drawing.Point(8, 168);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(144, 144);
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(200, 176);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 24);
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(200, 216);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(88, 24);
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(200, 256);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(88, 24);
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(5, 13);
this.ClientSize = new System.Drawing.Size(328, 325);
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 );

    }

 

    #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(96, 8);

      this.pictureBox1.Name = "pictureBox1";

      this.pictureBox1.Size = new System.Drawing.Size(144, 144);

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

      this.pictureBox1.TabIndex = 0;

      this.pictureBox1.TabStop = false;

      //

      // panel1

      //

      this.panel1.Location = new System.Drawing.Point(8, 168);

      this.panel1.Name = "panel1";

      this.panel1.Size = new System.Drawing.Size(144, 144);

      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(200, 176);

      this.button1.Name = "button1";

      this.button1.Size = new System.Drawing.Size(88, 24);

      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(200, 216);

      this.button2.Name = "button2";

      this.button2.Size = new System.Drawing.Size(88, 24);

      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(200, 256);

      this.button3.Name = "button3";

      this.button3.Size = new System.Drawing.Size(88, 24);

      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(5, 13);

      this.ClientSize = new System.Drawing.Size(328, 325);

      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();

    }

  }

}



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=722412

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:311371次
    • 积分:4310
    • 等级:
    • 排名:第7291名
    • 原创:79篇
    • 转载:33篇
    • 译文:55篇
    • 评论:103条
    文章分类
    最新评论