Click here to Skip to main content
14,212,407 members
Click here to Skip to main content
Article
Posted 5 Mar 2007

Stats

30.4K views
262 downloads
23 bookmarked

Image File Builder

,
Rate this:
2.61 (11 votes)
Please Sign up or sign in to vote.
2.61 (11 votes)
5 Mar 2007     CPOL    
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)

Share

About the Author

victorbos
Software Developer
United States United States
An old dog trying to learn new tricks!

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey16-Feb-12 1:57
professionalManoj Kumar Choubey16-Feb-12 1:57 
QuestionHow to make tranparent version of existing image file? Pin
CSharpian28-Mar-07 20:16
memberCSharpian28-Mar-07 20:16 
AnswerRe: How to make tranparent version of existing image file? Pin
victorbos29-Mar-07 3:52
membervictorbos29-Mar-07 3:52 
GeneralBad score Pin
Steve Hansen5-Mar-07 23:44
memberSteve Hansen5-Mar-07 23:44 
GeneralRe: Bad score Pin
victorbos17-Mar-07 15:17
membervictorbos17-Mar-07 15:17 

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

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

Layout: fixed | fluid

Article Copyright 2007 by victorbos
Everything else Copyright © CodeProject, 1999-2019

Server Web01
Version 2.8.190619.2