locked
how to get the image width and height! argh RRS feed

  • Question

  • User249648003 posted

    Hi all,

    I am trying to get the image height and width of an image I have obtained from a database and downloaded to an aspx page.

    I know I need to use something like:

    Dim Imagex As System.Drawing.Image = System.Drawing.Image.FromFile("~/RegisteredUsers/ShowPicture1.aspx")

    to declare the image but when I do it this way I get the following error

    Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

    Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

    So I figured it had somthing to do with not using the physical path.

    So I got the physical path like this:

    Dim physicalpath As String = Server.MapPath("~/RegisteredUsers/ShowPicture1.aspx")

    Then I go:

    Dim Imagex As System.Drawing.Image = System.Drawing.Image.FromFile(physicalpath)

    When I do it like this I get a different error:

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.OutOfMemoryException: Out of memory.

    So now I am lost, can anyone in the know help me on this?

    Thanks.

    Sunday, May 18, 2008 10:02 PM

Answers

  • User249648003 posted

    I got it, but I think I did it the hard way. But if anyone has the same problem, here is the solution I came up with.

     

    '******Connect to the database and bring back the main image Information******

    '******Resize the Image

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

    Const SQL As String = "SELECT [Pic1Ext], [Picture1] FROM [Listings] WHERE [ListingID] = @ListingID"

    Dim myCommand As New SqlCommand(SQL, myConnection)myCommand.Parameters.AddWithValue("@ListingID", StringGuid)

    myConnection.Open()

    Dim myReader As SqlDataReader = myCommand.ExecuteReader

    If myReader.Read Then

     

    End If

    Dim MainImage() As Byte

    MainImage = (CType(myReader.Item("Picture1"), Byte()))

    Dim MainMS As New System.IO.MemoryStream(MainImage)

    Dim origimage As System.Drawing.Image

    origimage = System.Drawing.Image.FromStream(MainMS)

    Dim OriginalWidth As Int16 = origimage.Width

    Dim OriginalHeight As Int16 = origimage.Height

    Dim SpaceHeight As Int16 = MainHeight

    Dim SpaceWidth As Int16 = MainWidth

    Dim PercentWidth As Decimal = (SpaceWidth / OriginalWidth)

    Dim PercentHeight As Decimal = (SpaceHeight / OriginalHeight)

    Dim nPercent As Decimal

    If PercentWidth < PercentHeight Then

    nPercent = PercentWidth

    Else

    nPercent = PercentHeight

    End If

    Image1.Width = origimage.Width * nPercent

    Image1.Height = origimage.Height * nPercent

    myReader.Close()

    myConnection.Close()

     

    End Using

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 24, 2008 10:19 PM

All replies

  • User1955095561 posted

    try this:

    FileStream fs = new FileStream(MapPath(ImageFolder + "/" + fileName),
              FileMode.Open, FileAccess.Read, FileShare.Read);
            
            System.Drawing.Image image = System.Drawing.Image.FromStream(fs)

    try
            {
              fileLength = Convert.ToInt32(fs.Length);
              fileWidth = image.Width;
              fileHeight = image.Height;
            }
    catch(Exception ex)

    {}

     

    Hope this helps.

    Monday, May 19, 2008 1:20 AM
  • User249648003 posted

    I am sorry, can you please put that in VB?

    Monday, May 19, 2008 2:18 AM
  • User249648003 posted

    Someone please help this is my code how do I get the image height and width so I can resize, also how do I resize??

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

    Dim ListingID As String = (Request.QueryString("GUID"))

    'Connect to the database and bring back the image contents & MIME type for the specified picture

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

    Const SQL As String = "SELECT [Pic1Ext], [Picture1] FROM [Listings] WHERE [ListingID] = @ListingID"

    'Const SQL As String = "SELECT [Pic1Ext], [Picture1] FROM [Listings] "

    Dim myCommand As New SqlCommand(SQL, myConnection)myCommand.Parameters.AddWithValue("@ListingID", ListingID)

    myConnection.Open()

    Dim myReader As SqlDataReader = myCommand.ExecuteReader

    If myReader.Read Then

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

    Response.BinaryWrite(myReader("Picture1"))

    End If

    myReader.Close()

    myConnection.Close()

    End Using

    End Sub

    Monday, May 19, 2008 5:27 AM
  • User1955095561 posted

    try out the code on this post to resize the image and also to get the width and the height.

    http://forums.asp.net/t/1259488.aspx

    also there is a link posted by me in this thread. That might also be helpful to you.
     

     

    Hope it helps 

    Monday, May 19, 2008 8:28 PM
  • User249648003 posted

    I got it, but I think I did it the hard way. But if anyone has the same problem, here is the solution I came up with.

     

    '******Connect to the database and bring back the main image Information******

    '******Resize the Image

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

    Const SQL As String = "SELECT [Pic1Ext], [Picture1] FROM [Listings] WHERE [ListingID] = @ListingID"

    Dim myCommand As New SqlCommand(SQL, myConnection)myCommand.Parameters.AddWithValue("@ListingID", StringGuid)

    myConnection.Open()

    Dim myReader As SqlDataReader = myCommand.ExecuteReader

    If myReader.Read Then

     

    End If

    Dim MainImage() As Byte

    MainImage = (CType(myReader.Item("Picture1"), Byte()))

    Dim MainMS As New System.IO.MemoryStream(MainImage)

    Dim origimage As System.Drawing.Image

    origimage = System.Drawing.Image.FromStream(MainMS)

    Dim OriginalWidth As Int16 = origimage.Width

    Dim OriginalHeight As Int16 = origimage.Height

    Dim SpaceHeight As Int16 = MainHeight

    Dim SpaceWidth As Int16 = MainWidth

    Dim PercentWidth As Decimal = (SpaceWidth / OriginalWidth)

    Dim PercentHeight As Decimal = (SpaceHeight / OriginalHeight)

    Dim nPercent As Decimal

    If PercentWidth < PercentHeight Then

    nPercent = PercentWidth

    Else

    nPercent = PercentHeight

    End If

    Image1.Width = origimage.Width * nPercent

    Image1.Height = origimage.Height * nPercent

    myReader.Close()

    myConnection.Close()

     

    End Using

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 24, 2008 10:19 PM
  • User-1171043462 posted

    Hi Do this way

    Bitmap bmp = new Bitmap(Server.MapPath("images/1.jpg"));

    int height = bmp.Height;

    int width = bmp.Width;

    Sunday, March 1, 2009 3:33 AM
  • User486328453 posted

    It is actually even simpler than that.

    Public Function GetImageWidth(ByVal ImgFile As String) As Integer
            Dim newImage As Image = Image.FromFile(ImgFile)
            Return newImage.Width
    End Function

    Then simply call the function from the page like so.

    <%If GetImageWidth(Server.MapPath("Photos\RSites\" & drRSites.Item("saLogo"))) > 200 Then%>

    or in whatever format you may need.

    Hope this is useful.

     

    Tuesday, November 17, 2009 7:10 PM
  • User-519136805 posted

    http://aliraza.wordpress.com/2008/02/05/how-to-get-uploaded-image-dimensions-in-aspnet/

    Wednesday, November 25, 2009 8:15 AM