Dynamically Resize an Image using Response.BinaryWrite RRS feed

  • Question

  • User-224188520 posted

    Hey All I've been mucking around with this for a bit now and can't seem to find a code example of how to resize an image based on how I am generating an image.

    I have banner images stored in an MS SQL database, the banners are selected for use at the top of webpages, when creating a new webpage the user is presented with a radio button list that has each banner generated in an image control. Currently I am just forcing the size of the image, with the image control and the page takes for ever for the images to load.

    How can I force a resize of my images at render time to be more like thumbnails?, bellow is the code I am using on my page load event of the page that renders the images.

    Imports System.Data.SqlClient

    Imports System.IO

    Partial Class imgDisplay

    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim banner_id As String = Request.QueryString("banner_id")

    Using ConnectionString As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)

    Const SQL As String = "SELECT [banner_img], [MIMEType] FROM [tbl_banner] WHERE [banner_id] = @banner_id"

    Dim myCommand As New SqlCommand(SQL, ConnectionString)myCommand.Parameters.AddWithValue("@banner_id", banner_id)


    Dim myReader As SqlDataReader = myCommand.ExecuteReader

    If myReader.Read Then

    Response.ContentType = myReader("MIMEType").ToString()


    End If



    End Using

    End Sub

    End Class


    Sunday, October 5, 2008 11:19 AM

All replies

  • User1396828672 posted

    This is a method I used in C#. Do you need it in VB?

    // this is how to call the resizeimage method from an upload form
    // I believe you can convert the myreader("banner_img") to a stream
    // and use the System.Drawing.Image.FromStream method
     private const int MAX_IMAGE_WIDTH_AND_HEIGHT = 200;

    byte[] fileArray = new Byte[profileImageUpload.PostedFile.ContentLength]; profileImageUpload.PostedFile.InputStream.Read(fileArray, 0, profileImageUpload.PostedFile.ContentLength); System.Drawing.Image uploadedImage = System.Drawing.Image.FromStream(profileImageUpload.PostedFile.InputStream); if (uploadedImage.Height > MAX_IMAGE_WIDTH_AND_HEIGHT || uploadedImage.Width > MAX_IMAGE_WIDTH_AND_HEIGHT) fileArray = ResizeImage(uploadedImage); private byte[] ResizeImage(System.Drawing.Image imageToResize) { Decimal percentageOfMaxWidth = (decimal)MAX_IMAGE_WIDTH_AND_HEIGHT / imageToResize.Width; Decimal percentageOfMaxHeight = (decimal)MAX_IMAGE_WIDTH_AND_HEIGHT / imageToResize.Height; Decimal whatPercentageToUse = percentageOfMaxWidth > percentageOfMaxHeight ? percentageOfMaxHeight : percentageOfMaxWidth; System.Drawing.Bitmap newImage = new System.Drawing.Bitmap(imageToResize, new System.Drawing.Size(Convert.ToInt32(imageToResize.Width * whatPercentageToUse), Convert.ToInt32(imageToResize.Height * whatPercentageToUse))); MemoryStream stream = new MemoryStream(); newImage.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] resizedImageArray = new Byte[stream.Length]; stream.Position = 0; stream.Read(resizedImageArray, 0, (int)stream.Length); stream.Close(); return resizedImageArray; }
    Sunday, October 5, 2008 5:34 PM
  • User-224188520 posted

    Thanks Chris, I can convert it to VB, no problem. Let me have a look at it tonight and I will let you know how it works out for me.

    Monday, October 6, 2008 3:59 PM
  • User-224188520 posted

    I used to think I was pretty good at figuring out code, but wow this one is really starting to bother me. I can not figure this out, there is a lot of code examples out there and they all seem massive just to do a simple little re-size job. Also most of the code out there deals with images stored in a file folder and not in a database.

    I am truely stumped on how to get a full sized image from my sql database, resize it to a much smaller version then display it using VB. Why does this seem so difficult and labour intensive. The code above shows the extraction of the full sized image.

    Tuesday, October 7, 2008 6:17 PM
  • User-224188520 posted

    Maybe I should re-phrase the question.

    Is there a way to get this


    Into an Image.FileStream so I then can use the many code examples to resize the image when being rendered from an Image.FileStream.

    Tuesday, October 7, 2008 8:59 PM