Library code snippets

Resizing images retrieved from SQL server

This is a nice neat way of resizing an image, I've simplified it and de-refactored (?) it for simplicity. Firstly, you ask for your image from the database:

SqlCommand cmd = new SqlCommand( "SELECT image FROM images WHERE id=@id" , connection);
cmd.Parameters.Add( "@id" , Request.QueryString[ "id" ]);
SqlDataReader dr = cmd.ExecuteReader();

Allocate an array of bytes to store it in temporarily:

byte[] image = null;
while (dr.Read())
{
  image = (byte[])dr.GetValue(0);
}
dr.Close()

Now you have an array of bytes that contains your image, you can freely load it into a bitmap from the array:

Bitmap b = (Bitmap)Bitmap.FromStream( new MemoryStream(image));

And you can resize that bitmap easily using the overloaded bitmap constructor:

Bitmap output = new Bitmap(b, new Size(320, 240);

One resized bitmap that you can now save or send anywhere - including Response.OutputStream!

Comments

  1. 28 May 2008 at 08:10

    I can't stand it when people ONLY thump their products on the forums.

    This article is a teaching Article and as such should be left in that realm. Your comments are of ONLY a commercial nature and NO VALUE to any one for learning. Except maybe that you are probably just as guilty of SPAMMING the world....Angry

     

  2. 02 Nov 2005 at 11:48

    The easiest way to upload and resize an image to the internet is I-Load.
    This component also create  an unlimited number of thumbnails  from the original image with the required size.
    I-Load is a FREE ASP.NET web control with numerous benefits and features.
    You can download I-Load (it's FREE!) and view an online demo here:


    http://www.radactive.com/en/Products/ILoad/Overview.aspx

  3. 24 Feb 2005 at 02:43

    Can u explain little more of resizing images when retrived from database with complete sample code /examples

  4. 01 Jan 1999 at 00:00

    This thread is for discussions of Resizing images retrieved from SQL server.

Leave a comment

Sign in or Join us (it's free).

Simon Soanes
AddThis

Related podcasts

Events coming up

  • Jul 22

    The Silicon Valley SQL Server User Group July Meetup

    Mountain View, United States

    Speaker: Christa Stelzmuller of MySpace Get an ?inside view? of the MySpace database infrastructure from the Chief Data Architect. You'll hear about the choices made by IT management, and the reasons behind their decisions. You will learn about:[list]Common challenges faced in scaling for a distributed data environment Approaches to managing transactions across a highly distributed data infrastructureHow MySpace administers many databases with only a few DBAs[/list]

We'd love to hear what you think! Submit ideas or give us feedback