[Help]PictureBox and Image properties[solved]

Posts 1–15 of 18 · Page 1 of 2
[Help]PictureBox and Image properties[solved]
Hey, i was wondering how to get an Image Size from the picturebox..

Im making an Image converter and have the PictureBox1.Image = OpenFileDialog yadda yadda yadda.

But how would i show the image size ? ( once the user selects the image, a label will show the image size)

Cheers for the answer's :-)
I think its this:

Code:
HeightLabel.Text = PictureBox1.Image.Height ' Get the height
WidthLabel.Text = PictureBox1.Image.Width ' Get the width
Hope it works.
Quote Originally Posted by Lyoto Machida View Post
I think its this:

Code:
HeightLabel.Text = PictureBox1.Image.Height ' Get the height
WidthLabel.Text = PictureBox1.Image.Width ' Get the width
Hope it works.
Yes that works. However

I have to choose an image first, then choose another image, then go back to the first one i chose before the label's display the heigh/width..

any idea how to fix?
Quote Originally Posted by AceKill3r View Post
Yes that works. However

I have to choose an image first, then choose another image, then go back to the first one i chose before the label's display the heigh/width..

any idea how to fix?
I didn understand but i think you need to put like this:
Code:
PictureBox1.Image = OpenFileDialog.Filename
HeightLabel.Text = PictureBox1.Image.Height ' Get the height
WidthLabel.Text = PictureBox1.Image.Width ' Get the width
In a button or something..
I think is that..
Quote Originally Posted by Lyoto Machida View Post


I didn understand but i think you need to put like this:
Code:
PictureBox1.Image = OpenFileDialog.Filename
HeightLabel.Text = PictureBox1.Image.Height ' Get the height
WidthLabel.Text = PictureBox1.Image.Width ' Get the width
In a button or something..
I think is that..
Code:
PictureBox1.Image = Image.FromFile(OpenFileDialog1.Filename)
xD
Quote Originally Posted by user590177 View Post
Code:
PictureBox1.Image = Image.FromFile(OpenFileDialog1.Filename)
xD
yeah that, i just copy and paste his code xD
Bare with me here, you're saying you want to select an image, then select another image, then go back to the first one and display it's width height?

Perhaps create a list of images, add to it, make the picturebox display the first image and set the label text in the very next step.

I'm still not 100% sure what your final purpose is though, why the need to open 2 images? A picturebox can only display 1 and can't hold any in memory...
Quote Originally Posted by Jason View Post
Bare with me here, you're saying you want to select an image, then select another image, then go back to the first one and display it's width height?

Perhaps create a list of images, add to it, make the picturebox display the first image and set the label text in the very next step.

I'm still not 100% sure what your final purpose is though, why the need to open 2 images? A picturebox can only display 1 and can't hold any in memory...
yea, maybe he means a listbox with images, and when you select an image it will show the image and the size.....

Try this:
Code:
    Public WithEvents timer1 As New Timer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x As New OpenFileDialog With {.Title = "Select image.."}
        x.ShowDialog()
        ListBox1.Items.Add(x.FileName)
        timer1.Start()
    End Sub

    Private Sub Timer1_Tick() Handles timer1.Tick
        Try
            PictureBox1.Image = Image.FromFile(ListBox1.SelectedItem.ToString)
            Label1.Text = PictureBox1.Image.Height
            Label2.Text = PictureBox1.Image.Width
        Catch ex As Exception

        End Try
    End Sub
Quote Originally Posted by Jason View Post
Bare with me here, you're saying you want to select an image, then select another image, then go back to the first one and display it's width height?

Perhaps create a list of images, add to it, make the picturebox display the first image and set the label text in the very next step.

I'm still not 100% sure what your final purpose is though, why the need to open 2 images? A picturebox can only display 1 and can't hold any in memory...
No,No,No - Thats what happenes.. i have to select 2 different image's for the labels to show the width & height.. ill put up a preview gimme 2 minutes
Quote Originally Posted by AceKill3r View Post
No,No,No - Thats what happenes.. i have to select 2 different image's for the labels to show the width & height.. ill put up a preview gimme 2 minutes
So, you want to display the width and height of 2 different images?
[highlight="vb"]' Load the Image
Dim objImage As System.Drawing.Image = _
System.Drawing.Image.FromFile("C:\MyImage.gif")

' Get Height and Width
Dim Width as integer = objImage.Width
Dim Height as integer = objImage.Height
[/highlight]

Bla, nvm <_<.


The video will sort of explain whats going on!
Quote Originally Posted by AceKill3r View Post


The video will sort of explain whats going on!
Hmm, what code are you using at this stage on the Button's "click" event, I should be able to figure it out from there. I think I know what the problem COULD be, I'll have to see the code before I'm sure though.
Here is all my code i've got so far.. the code is under ' TextBox1_TextChanged ' event..

[highlight=vb.netⓘ]

Public Class Form1
Dim OpenFileDialog1 As New OpenFileDialog
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
With OpenFileDialog1
.CheckFileExists = True
.ShowReadOnly = False
.Filter = "ICO(*.ico)|*.ico|JPG(*.jpg)|*.jpg*|BMP(*.bmp)|*.b mp|TIFF(*.tiff)|*.tiff|GIF(*.gif)|*.gif|PNG(*.png) |*.png"
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
TextBox1.Text = OpenFileDialog1.FileName
PictureBox3.Image = Image.FromFile(OpenFileDialog1.FileName)
End If
End With
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
If TextBox1.Text = "" Then
Dim OpenFileDialog1 As New OpenFileDialog
With OpenFileDialog1
.CheckFileExists = True
.ShowReadOnly = False
.Filter = "ICO(*.ico)|*.ico|JPG(*.jpg)|*.jpg*|BMP(*.bmp)|*.b mp|TIFF(*.tiff)|*.tiff|GIF(*.gif)|*.gif|PNG(*.png) |*.png"
.FilterIndex = 1
If .ShowDialog = DialogResult.OK Then
TextBox1.Text = OpenFileDialog1.FileName
' Load the specified file into a PictureBox control.
PictureBox3.Image = Image.FromFile(.FileName)
End If
End With
End If
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Button2.Enabled = True
Try
HeightLabel.Text = PictureBox3.Image.Height ' Get the height
WidthLabel.Text = PictureBox3.Image.Width ' Get the width
Catch ax As Exception
End Try
End Sub
Private Sub OpenImageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenImageToolStripMenuItem.Click
If TextBox1.Text = "" Then
MsgBox("Please select your image before clicking this!", MsgBoxStyle.Critical, "")
Else
Process.Start(TextBox1.Text)
End If
End Sub
Private Sub CloseMenuToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseMenuToolStripMenuItem.Click
ContextMenuStrip1.Hide()
End Sub
End Class
' ooo.Filter = "ICO(*.ico)|*.ico|JPG(*.jpg)|*.jpg*|BMP(*.bmp)|*.b mp|TIFF(*.tiff)|*.tiff|GIF(*.gif)|*.gif|PNG(*.png) |*.png"
[/highlight]
Okay, firstly remove the TextChanged event and all it's code completely, we'll handle it differently

Now remove your definition of "OpenFileDialog1" completely as well, we'll recode the Button2_Click sub to this:

[highlight=vb.net]
Using ofd As New OpenFileDialog With {.CheckFileExists = True, .ShowReadOnly = False, .Filter = "ICO(*.ico)|*.ico|JPG(*.jpg)|*.jpg*|BMP(*.bmp)|*.b mp|TIFF(*.tiff)|*.tiff|GIF(*.gif)|*.gif|PNG(*.png) |*.png"}
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim nextImg As Image = Image.FromFile(ofd.FileName)
HeightLabel.Text = nextImg.Height.ToString
WidthLabel.Text = nextImg.Width.ToString
PictureBox1.Image = nextImg
End If
End Using
[/highlight]

Now when you press okay in the OFD, it will set the pictureboxes image correctly, and display the image's height and width straight away. It's always best to call relevant code in the same event that you start from, rather than from another event which might not sync correctly.
Posts 1–15 of 18 · Page 1 of 2
This thread is closed for replies.
Talk with us