![](/Images/OutliningIndicators/ContractedBlock.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
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if( disposing )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if (components != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Windows Form Designer generated code#region Windows Form Designer generated code
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
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
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
Application.Run(new Form1());
}
private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(cp==null)
return;
for(float y=0;y<16;y++)
for(float x=0;x<16;x++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
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)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
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)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
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)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
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)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
//Creates a new GIF image with a modified colour palette
if(cp!=null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
//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
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
//steps through each pixel
for(int y=0;y<_gifImage.Height;y++)
for(int x=0;x<_gifImage.Width;x++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
//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)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
OpenFileDialog dlg=new OpenFileDialog();
dlg.Filter="GIF files|*.GIF";
if(dlg.ShowDialog()==DialogResult.OK)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
_gifImage=Image.FromFile(dlg.FileName);
this.pictureBox1.Image=_gifImage;
cp=_gifImage.Palette;
this.panel1.Invalidate();
}
}
private void button2_Click(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
SaveFileDialog dlg=new SaveFileDialog();
dlg.Filter="GIF files|*.gif";
dlg.DefaultExt=".gif";
dlg.AddExtension=true;
if(dlg.ShowDialog()==DialogResult.OK)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
_gifImage.Save(dlg.FileName,ImageFormat.Gif);
}
}
private void button3_Click(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
Application.Exit();
}
bool showTrans;
private void timer1_Tick(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
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();
}
}
}
-----------------------------------
![](/Images/OutliningIndicators/None.gif)
using System;
![](/Images/OutliningIndicators/None.gif)
using System.IO;
![](/Images/OutliningIndicators/None.gif)
using System.Drawing;
![](/Images/OutliningIndicators/None.gif)
using System.Drawing.Imaging;
![](/Images/OutliningIndicators/None.gif)
using System.Collections;
![](/Images/OutliningIndicators/None.gif)
using System.ComponentModel;
![](/Images/OutliningIndicators/None.gif)
using System.Windows.Forms;
![](/Images/OutliningIndicators/None.gif)
using System.Data;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
namespace TransparentGifCreator
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
![](/Images/OutliningIndicators/InBlock.gif)
/// Summary description for Form1.
![](/Images/OutliningIndicators/InBlock.gif)
/// </summary>
![](/Images/OutliningIndicators/InBlock.gif)
public class Form1 : System.Windows.Forms.Form
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.PictureBox pictureBox1;
![](/Images/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.Panel panel1;
![](/Images/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.Button button1;
![](/Images/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.Button button2;
![](/Images/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.Button button3;
![](/Images/OutliningIndicators/InBlock.gif)
private System.ComponentModel.IContainer components;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
Image _gifImage;
![](/Images/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.Timer timer1;
![](/Images/OutliningIndicators/InBlock.gif)
ColorPalette cp;
![](/Images/OutliningIndicators/InBlock.gif)
int CurrentEntry;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
public Form1()
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
//
![](/Images/OutliningIndicators/InBlock.gif)
// Required for Windows Form Designer support
![](/Images/OutliningIndicators/InBlock.gif)
//
![](/Images/OutliningIndicators/InBlock.gif)
InitializeComponent();
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//
![](/Images/OutliningIndicators/InBlock.gif)
// TODO: Add any constructor code after InitializeComponent call
![](/Images/OutliningIndicators/InBlock.gif)
//
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
![](/Images/OutliningIndicators/InBlock.gif)
/// Clean up any resources being used.
![](/Images/OutliningIndicators/InBlock.gif)
/// </summary>
![](/Images/OutliningIndicators/InBlock.gif)
protected override void Dispose( bool disposing )
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
if( disposing )
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
if (components != null)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
components.Dispose();
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
base.Dispose( disposing );
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Windows Form Designer generated code#region Windows Form Designer generated code
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
![](/Images/OutliningIndicators/InBlock.gif)
/// Required method for Designer support - do not modify
![](/Images/OutliningIndicators/InBlock.gif)
/// the contents of this method with the code editor.
![](/Images/OutliningIndicators/InBlock.gif)
/// </summary>
![](/Images/OutliningIndicators/InBlock.gif)
private void InitializeComponent()
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
this.components = new System.ComponentModel.Container();
![](/Images/OutliningIndicators/InBlock.gif)
this.pictureBox1 = new System.Windows.Forms.PictureBox();
![](/Images/OutliningIndicators/InBlock.gif)
this.panel1 = new System.Windows.Forms.Panel();
![](/Images/OutliningIndicators/InBlock.gif)
this.button1 = new System.Windows.Forms.Button();
![](/Images/OutliningIndicators/InBlock.gif)
this.button2 = new System.Windows.Forms.Button();
![](/Images/OutliningIndicators/InBlock.gif)
this.button3 = new System.Windows.Forms.Button();
![](/Images/OutliningIndicators/InBlock.gif)
this.timer1 = new System.Windows.Forms.Timer(this.components);
![](/Images/OutliningIndicators/InBlock.gif)
this.SuspendLayout();
![](/Images/OutliningIndicators/InBlock.gif)
//
![](/Images/OutliningIndicators/InBlock.gif)
// pictureBox1
![](/Images/OutliningIndicators/InBlock.gif)
//
![](/Images/OutliningIndicators/InBlock.gif)
this.pictureBox1.Location = new System.Drawing.Point(96, 8);
![](/Images/OutliningIndicators/InBlock.gif)
this.pictureBox1.Name = "pictureBox1";
![](/Images/OutliningIndicators/InBlock.gif)
this.pictureBox1.Size = new System.Drawing.Size(144, 144);
![](/Images/OutliningIndicators/InBlock.gif)
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
![](/Images/OutliningIndicators/InBlock.gif)
this.pictureBox1.TabIndex = 0;
![](/Images/OutliningIndicators/InBlock.gif)
this.pictureBox1.TabStop = false;
![](/Images/OutliningIndicators/InBlock.gif)
//
![](/Images/OutliningIndicators/InBlock.gif)
// panel1
![](/Images/OutliningIndicators/InBlock.gif)
//
![](/Images/OutliningIndicators/InBlock.gif)
this.panel1.Location = new System.Drawing.Point(8, 168);
![](/Images/OutliningIndicators/InBlock.gif)
this.panel1.Name = "panel1";
![](/Images/OutliningIndicators/InBlock.gif)
this.panel1.Size = new System.Drawing.Size(144, 144);
![](/Images/OutliningIndicators/InBlock.gif)
this.panel1.TabIndex = 1;
![](/Images/OutliningIndicators/InBlock.gif)
this.panel1.Click += new System.EventHandler(this.panel1_Click);
![](/Images/OutliningIndicators/InBlock.gif)
this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
![](/Images/OutliningIndicators/InBlock.gif)
this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseMove);
![](/Images/OutliningIndicators/InBlock.gif)
//
![](/Images/OutliningIndicators/InBlock.gif)
// button1
![](/Images/OutliningIndicators/InBlock.gif)
//
![](/Images/OutliningIndicators/InBlock.gif)
this.button1.Location = new System.Drawing.Point(200, 176);
![](/Images/OutliningIndicators/InBlock.gif)
this.button1.Name = "button1";
![](/Images/OutliningIndicators/InBlock.gif)
this.button1.Size = new System.Drawing.Size(88, 24);
![](/Images/OutliningIndicators/InBlock.gif)
this.button1.TabIndex = 2;
![](/Images/OutliningIndicators/InBlock.gif)
this.button1.Text = "Open";
![](/Images/OutliningIndicators/InBlock.gif)
this.button1.Click += new System.EventHandler(this.button1_Click);
![](/Images/OutliningIndicators/InBlock.gif)
//
![](/Images/OutliningIndicators/InBlock.gif)
// button2
![](/Images/OutliningIndicators/InBlock.gif)
//
![](/Images/OutliningIndicators/InBlock.gif)
this.button2.Location = new System.Drawing.Point(200, 216);
![](/Images/OutliningIndicators/InBlock.gif)
this.button2.Name = "button2";
![](/Images/OutliningIndicators/InBlock.gif)
this.button2.Size = new System.Drawing.Size(88, 24);
![](/Images/OutliningIndicators/InBlock.gif)
this.button2.TabIndex = 2;
![](/Images/OutliningIndicators/InBlock.gif)
this.button2.Text = "Save";
![](/Images/OutliningIndicators/InBlock.gif)
this.button2.Click += new System.EventHandler(this.button2_Click);
![](/Images/OutliningIndicators/InBlock.gif)
//
![](/Images/OutliningIndicators/InBlock.gif)
// button3
![](/Images/OutliningIndicators/InBlock.gif)
//
![](/Images/OutliningIndicators/InBlock.gif)
this.button3.Location = new System.Drawing.Point(200, 256);
![](/Images/OutliningIndicators/InBlock.gif)
this.button3.Name = "button3";
![](/Images/OutliningIndicators/InBlock.gif)
this.button3.Size = new System.Drawing.Size(88, 24);
![](/Images/OutliningIndicators/InBlock.gif)
this.button3.TabIndex = 2;
![](/Images/OutliningIndicators/InBlock.gif)
this.button3.Text = "Exit";
![](/Images/OutliningIndicators/InBlock.gif)
this.button3.Click += new System.EventHandler(this.button3_Click);
![](/Images/OutliningIndicators/InBlock.gif)
//
![](/Images/OutliningIndicators/InBlock.gif)
// timer1
![](/Images/OutliningIndicators/InBlock.gif)
//
![](/Images/OutliningIndicators/InBlock.gif)
this.timer1.Enabled = true;
![](/Images/OutliningIndicators/InBlock.gif)
this.timer1.Interval = 250;
![](/Images/OutliningIndicators/InBlock.gif)
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
![](/Images/OutliningIndicators/InBlock.gif)
//
![](/Images/OutliningIndicators/InBlock.gif)
// Form1
![](/Images/OutliningIndicators/InBlock.gif)
//
![](/Images/OutliningIndicators/InBlock.gif)
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
![](/Images/OutliningIndicators/InBlock.gif)
this.ClientSize = new System.Drawing.Size(328, 325);
![](/Images/OutliningIndicators/InBlock.gif)
this.Controls.Add(this.button1);
![](/Images/OutliningIndicators/InBlock.gif)
this.Controls.Add(this.panel1);
![](/Images/OutliningIndicators/InBlock.gif)
this.Controls.Add(this.pictureBox1);
![](/Images/OutliningIndicators/InBlock.gif)
this.Controls.Add(this.button2);
![](/Images/OutliningIndicators/InBlock.gif)
this.Controls.Add(this.button3);
![](/Images/OutliningIndicators/InBlock.gif)
this.Name = "Form1";
![](/Images/OutliningIndicators/InBlock.gif)
this.Text = "Form1";
![](/Images/OutliningIndicators/InBlock.gif)
this.ResumeLayout(false);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
![](/Images/OutliningIndicators/InBlock.gif)
/// The main entry point for the application.
![](/Images/OutliningIndicators/InBlock.gif)
/// </summary>
![](/Images/OutliningIndicators/InBlock.gif)
[STAThread]
![](/Images/OutliningIndicators/InBlock.gif)
static void Main()
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
Application.Run(new Form1());
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
if(cp==null)
![](/Images/OutliningIndicators/InBlock.gif)
return;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
for(float y=0;y<16;y++)
![](/Images/OutliningIndicators/InBlock.gif)
for(float x=0;x<16;x++)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
Color c=Color.Black;
![](/Images/OutliningIndicators/InBlock.gif)
if( ((16*y) + x)<cp.Entries.Length)
![](/Images/OutliningIndicators/InBlock.gif)
c=cp.Entries[(int)((16*y)+x)];
![](/Images/OutliningIndicators/InBlock.gif)
SolidBrush sb=new SolidBrush(Color.FromArgb(255,c));
![](/Images/OutliningIndicators/InBlock.gif)
float w=((float)this.panel1.Width)/16;
![](/Images/OutliningIndicators/InBlock.gif)
float h=((float)this.panel1.Height)/16;
![](/Images/OutliningIndicators/InBlock.gif)
e.Graphics.FillRectangle(sb,w*x,h*y,w,h);
![](/Images/OutliningIndicators/InBlock.gif)
if(c.A!=255)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
if(showTrans)
![](/Images/OutliningIndicators/InBlock.gif)
e.Graphics.DrawRectangle(Pens.Black,w*x,h*y,w-1,h-1);
![](/Images/OutliningIndicators/InBlock.gif)
else
![](/Images/OutliningIndicators/InBlock.gif)
e.Graphics.DrawRectangle(Pens.White,w*x,h*y,w-1,h-1);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
sb.Dispose();
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
private void panel1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
int y=(int)(((float)e.Y)/(((float)this.panel1.Width)/16f));
![](/Images/OutliningIndicators/InBlock.gif)
int x=(int)(((float)e.X)/(((float)this.panel1.Height)/16f));
![](/Images/OutliningIndicators/InBlock.gif)
CurrentEntry=(int)((16*y)+x);
![](/Images/OutliningIndicators/InBlock.gif)
if(cp!=null)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
if(CurrentEntry>=cp.Entries.Length)
![](/Images/OutliningIndicators/InBlock.gif)
CurrentEntry=cp.Entries.Length-1;
![](/Images/OutliningIndicators/InBlock.gif)
//Little bit of diagnostic for the palette chooser below
![](/Images/OutliningIndicators/InBlock.gif)
//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));
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
private void panel1_Click(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
//Creates a new GIF image with a modified colour palette
![](/Images/OutliningIndicators/InBlock.gif)
if(cp!=null)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
//Create a new 8 bit per pixel image
![](/Images/OutliningIndicators/InBlock.gif)
Bitmap bm=new Bitmap(_gifImage.Width,_gifImage.Height,PixelFormat.Format8bppIndexed);
![](/Images/OutliningIndicators/InBlock.gif)
//get it's palette
![](/Images/OutliningIndicators/InBlock.gif)
ColorPalette ncp=bm.Palette;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//copy all the entries from the old palette removing any transparency
![](/Images/OutliningIndicators/InBlock.gif)
int n=0;
![](/Images/OutliningIndicators/InBlock.gif)
foreach(Color c in cp.Entries)
![](/Images/OutliningIndicators/InBlock.gif)
ncp.Entries[n++]=Color.FromArgb(255,c);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//Set the newly selected transparency
![](/Images/OutliningIndicators/InBlock.gif)
ncp.Entries[CurrentEntry]=Color.FromArgb(0,cp.Entries[CurrentEntry]);
![](/Images/OutliningIndicators/InBlock.gif)
//re-insert the palette
![](/Images/OutliningIndicators/InBlock.gif)
bm.Palette=ncp;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//now to copy the actual bitmap data
![](/Images/OutliningIndicators/InBlock.gif)
//lock the source and destination bits
![](/Images/OutliningIndicators/InBlock.gif)
BitmapData src=((Bitmap)_gifImage).LockBits(new Rectangle(0,0,_gifImage.Width,_gifImage.Height),ImageLockMode.ReadOnly,_gifImage.PixelFormat);
![](/Images/OutliningIndicators/InBlock.gif)
BitmapData dst=bm.LockBits(new Rectangle(0,0,bm.Width,bm.Height),ImageLockMode.WriteOnly,bm.PixelFormat);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//uses pointers so we need unsafe code.
![](/Images/OutliningIndicators/InBlock.gif)
//the project is also compiled with /unsafe
![](/Images/OutliningIndicators/InBlock.gif)
unsafe
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
//steps through each pixel
![](/Images/OutliningIndicators/InBlock.gif)
for(int y=0;y<_gifImage.Height;y++)
![](/Images/OutliningIndicators/InBlock.gif)
for(int x=0;x<_gifImage.Width;x++)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
//transferring the bytes
![](/Images/OutliningIndicators/InBlock.gif)
((byte *)dst.Scan0.ToPointer())[(dst.Stride*y)+x]=((byte *)src.Scan0.ToPointer())[(src.Stride*y)+x];
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//all done, unlock the bitmaps
![](/Images/OutliningIndicators/InBlock.gif)
((Bitmap)_gifImage).UnlockBits(src);
![](/Images/OutliningIndicators/InBlock.gif)
bm.UnlockBits(dst);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//clear out the picturebox
![](/Images/OutliningIndicators/InBlock.gif)
this.pictureBox1.Image=null;
![](/Images/OutliningIndicators/InBlock.gif)
_gifImage.Dispose();
![](/Images/OutliningIndicators/InBlock.gif)
//set the new image in place
![](/Images/OutliningIndicators/InBlock.gif)
_gifImage=bm;
![](/Images/OutliningIndicators/InBlock.gif)
cp=_gifImage.Palette;
![](/Images/OutliningIndicators/InBlock.gif)
this.pictureBox1.Image=_gifImage;
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
private void button1_Click(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
OpenFileDialog dlg=new OpenFileDialog();
![](/Images/OutliningIndicators/InBlock.gif)
dlg.Filter="GIF files|*.GIF";
![](/Images/OutliningIndicators/InBlock.gif)
if(dlg.ShowDialog()==DialogResult.OK)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
_gifImage=Image.FromFile(dlg.FileName);
![](/Images/OutliningIndicators/InBlock.gif)
this.pictureBox1.Image=_gifImage;
![](/Images/OutliningIndicators/InBlock.gif)
cp=_gifImage.Palette;
![](/Images/OutliningIndicators/InBlock.gif)
this.panel1.Invalidate();
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
private void button2_Click(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
SaveFileDialog dlg=new SaveFileDialog();
![](/Images/OutliningIndicators/InBlock.gif)
dlg.Filter="GIF files|*.gif";
![](/Images/OutliningIndicators/InBlock.gif)
dlg.DefaultExt=".gif";
![](/Images/OutliningIndicators/InBlock.gif)
dlg.AddExtension=true;
![](/Images/OutliningIndicators/InBlock.gif)
if(dlg.ShowDialog()==DialogResult.OK)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
_gifImage.Save(dlg.FileName,ImageFormat.Gif);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
private void button3_Click(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
Application.Exit();
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
bool showTrans;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
private void timer1_Tick(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
showTrans^=true;
![](/Images/OutliningIndicators/InBlock.gif)
Graphics g=this.panel1.CreateGraphics();
![](/Images/OutliningIndicators/InBlock.gif)
//I do this rather than invalidate the panel because
![](/Images/OutliningIndicators/InBlock.gif)
//the panel draws its background ans so flickers horribly.
![](/Images/OutliningIndicators/InBlock.gif)
PaintEventArgs pe=new PaintEventArgs(g,new Rectangle(0,0,this.panel1.Width,this.panel1.Height));
![](/Images/OutliningIndicators/InBlock.gif)
this.panel1_Paint(this,pe);
![](/Images/OutliningIndicators/InBlock.gif)
g.Dispose();
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
posted @
2008-01-25 14:53
K!ngZ
阅读(
933)
评论()
编辑
收藏
举报