Results 1 to 2 of 2

Thread: [2008] Resize image - out of memory exception

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    180

    [2008] Resize image - out of memory exception

    I have this code

    Code:
    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

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2008] Resize image - out of memory exception

    Don't use GetThumbnail() to resize your images. Here's a better way that isn't as error prone as the GetThumbnail() method is.

    http://www.codeproject.com/KB/GDI-plus/imageresize.aspx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width