Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Image File Builder

0.00/5 (No votes)
5 Mar 2007 1  
A utility to quickly create opaque or transparent image files (*.GIF, *.PNG, etc.).

Screenshot - ImageFileBuilder.png

Introduction

If you need to build a small, simple image file quickly, this utility makes it quite easy. The image file can be transparent, semitransparent, or opaque (any color), and as small as 1 pixel by 1 pixel. The image file can be saved as a GIF, JPEG, PNG, TIF, etc.

Background

Every now and then, for a web project or WinForms project, one needs to create an image file, perhaps a 1x16_transparent.png or a 32x16_red.gif or a 100x1_blue.jpeg. There are countless ways to create these. ImageFileBuilder, perhaps a pompous sounding name, just made my life a little easier.

Using the code

If you have Visual Studio 2005, then you should be able to use the project source code "out of the box" -- simply build and run. The code itself is not rocket science. It is reasonably documented. A side benefit of the code for Bitmap newbies is, it shows you simple examples of how to create a Bitmap, how to color it, and how to use the ImageFormat enum to save the file in a format that matches the requested file extension.

Code for creating a Bitmap with a given color

public Bitmap CreateBitmap(int width, int height, Color clr)
{
  try
  {
      Bitmap bmp = new Bitmap(width, height);

      for (int y = 0; y < bmp.Height; y++)
      {
          for (int x = 0; x < bmp.Width; x++)
          {
              bmp.SetPixel(x, y, clr);
          }
      }

      return bmp;
  }
  catch (Exception ex)
  {
      this.DoUpdateErrorMsg(ex.ToString());
      Debug.WriteLine(ex.ToString());
      return null;
  }
}

Code for saving a Bitmap with different file types

public bool SaveBitmap(Bitmap bmp, string sOutputFilename)
{
  bool bRet = false;
  try
  {
      FileInfo fiOutputFile = new FileInfo(sOutputFilename);

      //determine the image format from the file name
      ImageFormat imgFmtWant = ImageFormat.Png;
      switch (fiOutputFile.Extension.ToUpper())
      {
          case ".BMP" : imgFmtWant = ImageFormat.Bmp; break;
          case ".EMF" : imgFmtWant = ImageFormat.Emf; break;
          case ".EXF" :
          case ".EXIF": imgFmtWant = ImageFormat.Exif; break;
          case ".GIF" : imgFmtWant = ImageFormat.Gif; break;
          case ".ICO" : imgFmtWant = ImageFormat.Icon; break;
          case ".JPG" :
          case ".JPEG": imgFmtWant = ImageFormat.Jpeg; break;
          case ".PNG" : imgFmtWant = ImageFormat.Png; break;
          case ".TIF" :
          case ".TIFF": imgFmtWant = ImageFormat.Tiff; break;
          case ".WMF" : imgFmtWant = ImageFormat.Wmf; break;
          default: // none of the above, so add PNG to the name
              sOutputFilename += ".png";
              this.DoUpdateErrorMsg("WARNING: Output file " + 
                                    "name modified; Extension '.png' added.");
              break;
      }

      bmp.Save(sOutputFilename, imgFmtWant);
      bRet = true;
  }
  catch (Exception ex)
  {
      this.msErrorMsg += this.msEOL + ex.ToString();
      bRet = false;
  }
  return bRet;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here