Help with picturebox height, width and file size.

I came into some trouble with getting dimensions and file size from a

Page 1 of 1

4 Replies - 18700 Views - Last Post: 13 August 2008 - 01:27 PM Rate Topic: -----

#1 EvolutionMedia  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 125
  • Joined: 11-August 08

Help with picturebox height, width and file size.

Post icon  Posted 13 August 2008 - 10:48 AM

Hey!

I know how to check a file size on a file on the computer. However, I'm mostly dealing with URLs.

I was hoping that maybe load it into a web browser compenent then getting the file size-but that might not work. Also shouldn't the net function do anything?

Also, I ran the program and when it came time to tell me the size or the dimensions (heigh, width) it said the object reference is not a instance to a object.

This is what I did to try get the dimensions:


Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
		
		Dim fileSize As Integer

		PictureBox1.ImageLocation = ListView1.FocusedItem.Text ' Grab the image from the URL that is put into the array.
		
		 Height_Size.Text = PictureBox1.Image.Height.ToString ' Insert the height of picture into the height textbox.


		Width_Size.Text = PictureBox1.Image.Width.ToString ' Insert the width of picture into the width textbox.

		fileSize = Len(My.Computer.FileSystem.GetFileInfo(PictureBox1.ImageLocation)) 'Grab the size of the image from URL.


		Pic_Size.Text = fileSize 'Put in the filesize into the filesize textbox.


	End Sub




THanks guys for your great support! :)

Is This A Good Question/Topic? 0
  • +

Replies To: Help with picturebox height, width and file size.

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Help with picturebox height, width and file size.

Posted 13 August 2008 - 11:19 AM

The way I would accomplish this, since you're loading the image into a PictureBox with a valid URL, would be to create a new Image object based on the URL, then use the Height (or Width) property of that image. Something like

GetImageHeight:
Private Function GetImageHeight(ByRef file As String) As Integer
	Using image As System.Drawing.Image = System.Drawing.Image.FromFile(file)
		Return image.Height
	End Using
End Function




GetImageWidth:
Private Function GetImageWidth(ByRef file As String) As Integer
	Using image As System.Drawing.Image = System.Drawing.Image.FromFile(file)
		Return image.Width
	End Using
End Function




Then in the code you're using:


Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged        
    Dim fileSize As Integer
    PictureBox1.ImageLocation = ListView1.FocusedItem.Text ' Grab the image from the URL that is put into the array.        
     Height_Size.Text = GetImageHeight(ListView1.FocusedItem.Text) ' Insert the height of picture into the height textbox.

    Width_Size.Text = GetImageWidth(ListView1.FocusedItem.Text)  ' Insert the width of picture into the width textbox.
    fileSize = Len(My.Computer.FileSystem.GetFileInfo(PictureBox1.ImageLocation)) 'Grab the size of the image from URL.
    Pic_Size.Text = fileSize 'Put in the filesize into the filesize textbox.
End Sub




That should give you the height & width of the image. Getting the size I would create a new FileInfo based on the image in the PictureBox, something like this:


Private Function GetImageSize(ByRef file As String) As Double
	Dim info As New FileInfo(file)
	Return info.Length / 1024
End Function




Then I would change the following like

fileSize = Len(My.Computer.FileSystem.GetFileInfo(PictureBox1.ImageLocation))



To

fileSize = GetImageSize(ListView1.FocusedItem.Text)



There may be some modifications that need to occur since you're dealing with URL's and not physical images (such as where my code comes from), let me know what happens :)
Was This Post Helpful? 0
  • +
  • -

#3 EvolutionMedia  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 125
  • Joined: 11-August 08

Re: Help with picturebox height, width and file size.

Posted 13 August 2008 - 12:30 PM

Hey! It says URI are not supported on the line:

Using image As System.Drawing.Image = System.Drawing.Image.FromFile(file)



I tried to change the FromFile to FromHbitmap and it says can't not convert String to long. :angry:

There has to be away-I'll look into it but if any one else has any other ideas that would be great!

P3ace!
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Help with picturebox height, width and file size.

Posted 13 August 2008 - 12:56 PM

Thats because I type before thinking. You have to create an HttpWebRequest and use Image.FromStream. These changes should solve your problem


GetImageHeight
Private Function GetImageHeight(ByRef file As String) As Integer
	Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(System.Net.HttpWebRequest.Create(file).GetResponse().GetResponseStream()))
	Return image.Height
End Function




GetImageWidth
Private Function GetImageWidth(ByRef file As String) As Integer
	Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(System.Net.HttpWebRequest.Create(file).GetResponse().GetResponseStream()))
		Return image.Width
End Function




Sorry about that. By the way, I just tested it with an image URL in a Windows Application and it loaded the image into the picture box :)
Was This Post Helpful? 0
  • +
  • -

#5 EvolutionMedia  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 125
  • Joined: 11-August 08

Re: Help with picturebox height, width and file size.

Posted 13 August 2008 - 01:27 PM

View PostPsychoCoder, on 13 Aug, 2008 - 12:56 PM, said:

Thats because I type before thinking. You have to create an HttpWebRequest and use Image.FromStream. These changes should solve your problem


GetImageHeight
Private Function GetImageHeight(ByRef file As String) As Integer
	Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(System.Net.HttpWebRequest.Create(file).GetResponse().GetResponseStream()))
	Return image.Height
End Function




GetImageWidth
Private Function GetImageWidth(ByRef file As String) As Integer
	Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(System.Net.HttpWebRequest.Create(file).GetResponse().GetResponseStream()))
		Return image.Width
End Function




Sorry about that. By the way, I just tested it with an image URL in a Windows Application and it loaded the image into the picture box :)


Hey!

Thanks, it works-however, one small problem. It seems not to be able to read the file size. So I modified the code to look something like this.

	Private Function Get_ImageSize(ByRef file As String) As Double

		Dim info = System.Net.HttpWebRequest.Create(file).GetResponse().GetResponseStream()
		Dim size = System.Net.HttpWebRequest.Create(file).ContentLength

		size = size / 1024

		Return size

	End Function



However, it displays a 0. Also, it seems to be alitlte slow. Is tehre any way to optimize the code so it reads faster? Thanks!

Plus how do I add you as a friend on here?


View PostPsychoCoder, on 13 Aug, 2008 - 12:56 PM, said:

Thats because I type before thinking. You have to create an HttpWebRequest and use Image.FromStream. These changes should solve your problem


GetImageHeight
Private Function GetImageHeight(ByRef file As String) As Integer
	Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(System.Net.HttpWebRequest.Create(file).GetResponse().GetResponseStream()))
	Return image.Height
End Function




GetImageWidth
Private Function GetImageWidth(ByRef file As String) As Integer
	Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(System.Net.HttpWebRequest.Create(file).GetResponse().GetResponseStream()))
		Return image.Width
End Function




Sorry about that. By the way, I just tested it with an image URL in a Windows Application and it loaded the image into the picture box :)


Okay, I'm able to get the picture's width, height, and file size. The Get_Filesize function looks more like this with some modifications.

 Private Function Get_ImageSize(ByRef file As String) As Integer


		Dim info = System.Net.HttpWebRequest.Create(file).GetResponse().GetResponseStream()
		Dim size = System.Net.HttpWebRequest.Create(file).GetResponse().ContentLength()



		size = size / 1024

		Return size

	End Function



Sorry, about it - the damn thing was staring at my face! LOL!

However, any tricks to optimize loading? thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1