Click here to Skip to main content
Licence CPOL
First Posted 5 Mar 2007
Views 17,903
Downloads 97
Bookmarked 19 times

Image File Builder

By | 5 Mar 2007 | Article
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

victorbos

Software Developer

United States United States

Member

An old dog trying to learn new tricks!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembermanoj kumar choubey1:57 16 Feb '12  
QuestionHow to make tranparent version of existing image file? PinmemberCSharpian20:16 28 Mar '07  
AnswerRe: How to make tranparent version of existing image file? Pinmembervictorbos3:52 29 Mar '07  
GeneralBad score PinmemberSteve Hansen23:44 5 Mar '07  
GeneralRe: Bad score Pinmembervictorbos15:17 17 Mar '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 5 Mar 2007
Article Copyright 2007 by victorbos
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid