Resize image - out of memory exception

MondeoST24

Member
Joined
Mar 9, 2006
Messages
16
Programming Experience
Beginner
Hi,

I have this code

VB.NET:
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.Drawing.Imaging

Partial Class image2
    Inherits System.Web.UI.Page

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

        Dim dbType As String = Request.QueryString("type")
        Dim width As String = Request.QueryString("width")
        Dim height As String = Request.QueryString("height")
        Dim capID As String = Request.QueryString("capid")

        Dim sqlCmd As String = String.Format("SELECT ima_image FROM NVDImage A INNER JOIN NVDModelYear B ON A.ima_imageid = b.My_ImageID WHERE b.my_id = {0} ORDER BY B.MY_EffectiveTo", capID)
        Dim sqlConn As SqlConnection
        Select Case dbType
            Case "car"
                sqlConn = New SqlConnection("myConnString")
            Case Else
                sqlConn = New SqlConnection("myConnString")
        End Select
        Dim cmd As New SqlCommand(sqlCmd, sqlConn)
        Dim b() As Byte = Nothing
        sqlConn.Open()

        b = CType(cmd.ExecuteScalar(), Byte())

        sqlConn.Close()
        If b.Length > 0 Then
            Dim stream As New System.IO.MemoryStream(b, True)
            stream.Write(b, 0, b.Length)
            Dim oldBMP As image = image.FromStream(stream)
            stream.Close()

            
            Dim convetedImage As image = ConvertImage(oldBMP, Integer.Parse(width), Integer.Parse(height))
            convetedImage.Save(Response.OutputStream, ImageFormat.Jpeg)
            Response.End()
        End If
    End Sub

   
    Private Function ConvertImage(ByVal img As Image, ByVal targetWidth As Integer, ByVal targetHeight As Integer) As Image

        Dim del As New image.GetThumbnailImageAbort(AddressOf isAborted)
        Dim ww As Double = (1 * targetWidth) / (1 * img.Width)
        Dim hh As Double = (1 * targetHeight) / (1 * img.Height)
        Dim k As Double = Math.Min(hh, ww)
        Dim thW As Double = k * img.Width
        Dim thH As Double = k * img.Height
        Dim thumb As image = img.GetThumbnailImage(CInt(thW), CInt(thH), del, IntPtr.Zero)
        Dim bmp As New Bitmap(targetWidth, targetHeight)

        Dim gr As Graphics = Graphics.FromImage(bmp)
        gr.FillRectangle(New SolidBrush(Color.White), 0, 0, bmp.Width, bmp.Height)

        Dim distFromLeft As Double = (targetWidth - thW) / 2
        Dim distFromTop As Double = (targetHeight - thH) / 2

        gr.DrawImage(thumb, CSng(distFromLeft), CSng(distFromTop), thumb.Width, thumb.Height)
        gr.Flush()

        Return bmp
    End Function

    Private Function isAborted() As Boolean
        Return False
    End Function


End Class

It fails on this line

Dim thumb As image = img.GetThumbnailImage(CInt(thW), CInt(thH), del, IntPtr.Zero)

System.OutOfMemoryException: Out of memory.

What could the problem be?

Thanks
 
This part is wrong for several reasons:
Dim stream As New System.IO.MemoryStream(b, True)
stream.Write(b, 0, b.Length)
Dim oldBMP As image = image.FromStream(stream)
stream.Close()
change to:
VB.NET:
Dim oldBMP As image = Image.FromStream(New System.IO.MemoryStream(b))
 
Thanks for that, when I make that change I get garbled text output. I presume its the binary contents of the file - how can I get it to output properly?

Thanks
 
Try setting the response content type to image/jpeg:
Response.ContentType = "image/jpeg"
 
Back
Top