Intro to the System.Drawing.Bitmap Class in .Net

The System.Drawing.Bitmap class encapsulates a GDI+ bitmap. The heck’s a GDI+ bitmap? It’s just a data structure that represents a graphical image based on pixels, which you can basically think of as a grid made up of tiny dots.

Each tiny dot has properties that can be controlled and manipulated, such as it’s color and transparency level.

GDI+ is simply the latest version of GDI (Graphics Device Interface), a Windows component that takes care of rendering graphics on devices (monitor, printer, etc).

Loading an image as Bitmap

After doing some work with the map, you might want to save it.

Saving a Bitmap

You can save to different image formats…

Image Encoding Formats

Encoders translate the data in an Image or Bitmap object into one of these designated format:

  • Bmp
  • Gif
  • Jpeg
  • Png
  • Tiff
  • Exif
  • Guid
  • Icon
  • MemoryBmp
  • EMF/Wmf
Encoding Parameters

You can control how encoders do their thing by explicitly setting encoding parameters, such as quality, color depth, compression, etc. They are available for the following formats.

  • Bmp
  • Gif
  • Jpeg
  • Png
  • Tiff
Simple Save Example

Simple Save with Encoding Example

ActionResult Stream Example

ActionResult Stream With Encoding Example

Creating a custom ActionResult that serves images

Then, you can call use it like this:

And from the view:

These are excellent ways to serve images around authentication or permissions. You can check to validate things in the controller, then handle it appropriately.

These were my notes from watching the following series:

Hello world!

Once upon a time…