locked
get image height and width without creating bitmap RRS feed

  • Question

  • User-881450349 posted

    I need to get the height and width of an image that is in a directory on the server. I have been doing it by createing a bitmap and then checking the height and width of the bitmap and then disposing it. But for some reason something lingers and if I do it twice in quick succession then I get an error.

    Is there a better way to do this? I am thinking I need to do it once when the image is uploaded to the server and then store the height and width in a database for later use. Is there a simpler or better way than this?


    Thanks,

    Sam

    Tuesday, August 18, 2009 10:23 AM

Answers

  • User-720791821 posted
     Dim imageurl As String = Server.MapPath("/uploads/" + filename)
    Dim fullSizeImg As System.Drawing.Image = System.Drawing.Image.FromFile(imageurl)

    Dim width As Integer = fullSizeImg.Width
    Dim height As Integer = fullSizeImg.Height
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 18, 2009 12:21 PM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 18, 2009 12:28 PM

All replies

  • User-720791821 posted
     Dim imageurl As String = Server.MapPath("/uploads/" + filename)
    Dim fullSizeImg As System.Drawing.Image = System.Drawing.Image.FromFile(imageurl)

    Dim width As Integer = fullSizeImg.Width
    Dim height As Integer = fullSizeImg.Height
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 18, 2009 12:21 PM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 18, 2009 12:28 PM
  • User-1763871356 posted

    Any chance you're reassigning your Bitmap reference to a new Bitmap before disposing it? I had an issue once where I created a Bitmap bmp from a file. I then did something like bmp = bmp.GetThumbnailImage() which left the filestream to the original file open. It is strange that the class does not close the filestream before returning to the caller.

    Also there are some alternatives to using GDI+, such as reading file properties, here: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/94430444-283b-4a0e-9ca5-7375c8420622 

    Wednesday, August 26, 2009 11:20 AM