September 2, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Rendering Images in ASP.NET Directly from Your Database

  • August 1, 2007
  • By Paul Kimmel
  • Send Email »
  • More Articles »

Listing 7: Code that scales the images (but keep in mind that the GIF images didn't render well after manipulation; the fault may be mine though).

Const max  As Integer = 64
Dim width  As Integer
Dim height As Integer
Dim scalar As Single

If (FImage.Width > FImage.Height) Then
   width  = max
   scalar = width / FImage.Width
   height = scalar * FImage.Height
   height = max
   scalar = height / FImage.Height
   width  = scalar * FImage.Width
End If

Dim bmp As Bitmap = New Bitmap(width, height)
Dim g As Graphics = Graphics.FromImage(bmp)
g.DrawImage(FImage, 0, 0, bmp.Width, bmp.Height)
Dim stream As MemoryStream = New MemoryStream()
bmp.Save(stream, ImageFormat.Gif)

Listing 8: You have a perfectly valid canvas to write on, so you can use and of the GDI+ capabilities when you manage loading the images from the database.

Dim b As Bitmap   = New Bitmap(FImage.Width, FImage.Height)
Dim g As Graphics = Graphics.FromImage(b)
g.DrawImage(FImage, 0, 0)
Dim f As Font = New Font(FontFamily.GenericSansSerif, 12, _
g.DrawString("Extra Information", f, Brushes.Red, 10, 10)
Dim stream As MemoryStream = New MemoryStream()
b.Save(stream, ImageFormat.Gif)

Finally, it is worth noting that you could store the path to the image in the database, use Image.LoadFromStream—Image.LoadFromFile locks images—and manipulate the image. Then, you could use the techniques described in this article to render the image. The main premise is why should images be stored differently when they don't have to be?


This article demonstrates how to store and retrieve images from a SQL Server database instead of storing the path and binding the URL. The technique renders the image to a GDI+ object and stuffs the results in the HttpResponse stream. Then, instead of setting the Image.ImageUrl (or <img> tag's src attribute) to the path, you assign the relevant attribute to the .aspx page containing the rendered image. The result is that you now store images in a way that is homogenous with other kinds of data, and you are free to manipulate the image—by scaling, rotating, annotating, for example—any way that images in general can be manipulated by GDI+.

Final Word: If you want an excellent read on general business information, read Geoffrey A. Moore's Dealing With Darwin: How Great Companies Innovate at Every Phase of Their Evolution.

About the Author

Paul Kimmel is the VB Today columnist for and has written several books on object-oriented programming and .NET. Check out his new book UML DeMystified from McGraw-Hill/Osborne. Paul is a software architect for Tri-State Hospital Supply Corporation. You may contact him for technology questions at

If you are interested in joining or sponsoring a .NET Users Group, check out

Copyright © 2007 by Paul Kimmel All Rights Reserved.

Page 5 of 5

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Sitemap | Contact Us

Rocket Fuel