Jump to content

'image' is not a member of 'System.Drawing.Image'

CripDawg

i have the current code and am getting this error 'image' is not a member of 'System.Drawing.Image' and have no idea how to fix it any help would be appreciated

 Public Class Help1     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        Dim PictureBox1 As System.Drawing.Image         Select Case PictureBox1            Case PictureBox1.Image = My.Resources.Serach_1          End Select    End SubEnd Class
Link to comment
Share on other sites

Link to post
Share on other sites

You might also need a project reference to System.Drawing 

Link to comment
Share on other sites

Link to post
Share on other sites

Not sure if this has got anything to do with it but is "Serach_1" meant to be "Search_1"?

 

or maybe you select block might be wrong 

Select Case PictureBox1.Image    Case My.Resources.Serach_1          'Do stuff











 

 

You might also need a project reference to System.Drawing 

nope these do not work get the same error 

Link to comment
Share on other sites

Link to post
Share on other sites

This line is confusing.

Dim PictureBox1 As System.Drawing.Image

System.Drawing.Image is an abstract class so it needs to be initialized. You need to assign something to it before you use it.

Dim PictureBox1 As System.Drawing.ImagePictureBox1 = ...//or simplyDim PictureBox1 As System.Drawing.Image = ...// this is invalid due to it being an abstract classDim PictureBox1 As New System.Drawing.Image// and it doesn't have an Image property so // PictureBox1.Image// is invalid// you'd use it in the select case by just the nameSelect Case PictureBox1    Case My.Resources.Serach_1...

However, because you name it PictureBox1 and try to access an Image property, that leads me to believe what you want is a System.Windows.Forms.PictureBox control.

 

If you added a PictureBox to your form by the designer, then you can remove the Dim PictureBox1 As System.Drawing.Image line from your code as it's already been created for you by the designer.

 

That leaves you with

 Public Class Help1     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        Select Case PictureBox1.Image // Fixed this            Case My.Resources.Serach_1                ...         End Select    End SubEnd Class

If you don't have a PictureBox added to your form from the designer, then you need to create one. Either add it to the designer or manually create it in code behind with Dim PictureBox1 As New System.Windows.Forms.PictureBox

Link to comment
Share on other sites

Link to post
Share on other sites

This line is confusing.

Dim PictureBox1 As System.Drawing.Image

System.Drawing.Image is an abstract class so it needs to be initialized. You need to assign something to it before you use it.

Dim PictureBox1 As System.Drawing.ImagePictureBox1 = ...//or simplyDim PictureBox1 As System.Drawing.Image = ...// this is invalid due to it being an abstract classDim PictureBox1 As New System.Drawing.Image// and it doesn't have an Image property so // PictureBox1.Image// is invalid// you'd use it in the select case by just the nameSelect Case PictureBox1    Case My.Resources.Serach_1...

However, because you name it PictureBox1 and try to access an Image property, that leads me to believe what you want is a System.Windows.Forms.PictureBox control.

 

If you added a PictureBox to your form by the designer, then you can remove the Dim PictureBox1 As System.Drawing.Image line from your code as it's already been created for you by the designer.

 

That leaves you with

 Public Class Help1     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        Select Case PictureBox1.Image // Fixed this            Case My.Resources.Serach_1                ...         End Select    End SubEnd Class

If you don't have a PictureBox added to your form from the designer, then you need to create one. Either add it to the designer or manually create it in code behind with Dim PictureBox1 As New System.Windows.Forms.PictureBox

so i did create a picturebox in my designer but i have this error Operator '=' is not defined for types 'System.Drawing.Image' and 'System.Drawing.Bitmap'.  using the code below PS i renamed the picturebox to steps 

Public Class Help1     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        Select Case Steps.Image            Case My.Resources.Serach_1          End Select    End SubEnd Class
Link to comment
Share on other sites

Link to post
Share on other sites

Hmm, looks like Image can't be compared against other Image objects in that way.

 

You can use an If statement with Steps.Image.Equals(My.Resources.Serach_1) however that's only good if you are testing reference equality (maybe that's good enough for you, I don't know).

 

But if you need to actually test if two different images are the same, you need to compare them pixel by pixel manually.

Link to comment
Share on other sites

Link to post
Share on other sites

Hmm, looks like Image can't be compared against other Image objects in that way.

 

You can use an If statement with Steps.Image.Equals(My.Resources.Serach_1) however that's only good if you are testing reference equality (maybe that's good enough for you, I don't know).

 

But if you need to actually test if two different images are the same, you need to compare them pixel by pixel manually.

so the whole point of this button is to display a image depending on the current image its like a next button but when i tried this code the button does nothing 

Public Class Help1     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        If Steps.Image.Equals(My.Resources.Serach_1) Then            Steps.Image.Equals(My.Resources.Serach_2)        ElseIf Steps.Image.Equals(My.Resources.Serach_2) Then            Steps.Image.Equals(My.Resources.Serach_3)        End If    End SubEnd Class
Link to comment
Share on other sites

Link to post
Share on other sites

Equals() is for comparison since it returns a Boolean. It's not for assignment.

 

Try

Public Class Help1     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        If Steps.Image.Equals(My.Resources.Serach_1) Then            Steps.Image = My.Resources.Serach_2        ElseIf Steps.Image.Equals(My.Resources.Serach_2) Then            Steps.Image = My.Resources.Serach_3        Else            Debug.Print("No images are equal")        End If    End SubEnd Class

edit: I added the Else block so if Equals() isn't working for you, it'll print out the debug message if you run the app in Debug mode (not Release mode).

Link to comment
Share on other sites

Link to post
Share on other sites

Equals() is for comparison since it returns a Boolean. It's not for assignment.

 

Try

Public Class Help1     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        If Steps.Image.Equals(My.Resources.Serach_1) Then            Steps.Image = My.Resources.Serach_2        ElseIf Steps.Image.Equals(My.Resources.Serach_2) Then            Steps.Image = My.Resources.Serach_3        Else            Debug.Print("No images are equal")        End If    End SubEnd Class

edit: I added the Else block so if Equals() isn't working for you, it'll print out the debug message if you run the app in Debug mode (not Release mode).

it prints No images are equal

Link to comment
Share on other sites

Link to post
Share on other sites

Then Equals() isn't going to work for you as stated above. You'll need to manually compare them pixel by pixel or find another way to tell what image is currently shown.

 

It's easier to just find another way.

// for example: using a variablePrivate imageId As IntegerimageId = 0 // means no imageimageId = 1 // means Serach_1imageId = 2 // means Serach_2imageId = 3 // means Serach_3
Link to comment
Share on other sites

Link to post
Share on other sites

 

Then Equals() isn't going to work for you as stated above. You'll need to manually compare them pixel by pixel or find another way to tell what image is currently shown.

 

It's easier to just find another way.

// for example: using a variablePrivate imageId As IntegerimageId = 0 // means no imageimageId = 1 // means Serach_1imageId = 2 // means Serach_2imageId = 3 // means Serach_3

how do i specify that imageId 1 =  search_1  and so on

Link to comment
Share on other sites

Link to post
Share on other sites

You can use a simple If or Select statement

    Private _imageId As Integer    Private Const MaxId As Integer = 3    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click        // Increment ID by one so it's the ID of the next image        // Check if it's at the last image, and if so, go back        // to the first image. (if you want to do that)        If _imageId = MaxId Then            _imageId = 1        Else            _imageId += 1        End If        // Set the image based on ID        SetImage(PictureBox1, _imageId)    End Sub    Public Shared Sub SetImage(pb As PictureBox, id As Integer)        Select Case id            Case 1                pb.Image = Nothing // Replace Nothing with My.Resource.Name            Case 2                pb.Image = Nothing            Case 3                pb.Image = Nothing            Case Else                Debug.Print("Invalid")        End Select    End Sub

Alternatively, instead of an integer, you can change this to work with enums or arrays or whatever you want but you can look into that stuff yourself if you feel like it.

Link to comment
Share on other sites

Link to post
Share on other sites

You can use a simple If or Select statement

    Private _imageId As Integer    Private Const MaxId As Integer = 3    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click        // Increment ID by one so it's the ID of the next image        // Check if it's at the last image, and if so, go back        // to the first image. (if you want to do that)        If _imageId = MaxId Then            _imageId = 1        Else            _imageId += 1        End If        // Set the image based on ID        SetImage(PictureBox1, _imageId)    End Sub    Public Shared Sub SetImage(pb As PictureBox, id As Integer)        Select Case id            Case 1                pb.Image = Nothing // Replace Nothing with My.Resource.Name            Case 2                pb.Image = Nothing            Case 3                pb.Image = Nothing            Case Else                Debug.Print("Invalid")        End Select    End Sub

Alternatively, instead of an integer, you can change this to work with enums or arrays or whatever you want but you can look into that stuff yourself if you feel like it.

so i tried this and get the error Argument not specified for parameter 'id' of 'Public Shared Sub SetImage(pb As System.Windows.Forms.PictureBox, id As Integer)'. also isnt there meant to be soething before the select statement that increments the id integer or not

 

 Public Class Help1      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        SetImage(Steps)    End Sub    Public Shared Sub SetImage(ByVal pb As PictureBox, ByVal id As Integer)        Select Case id            Case 1                pb.Image = My.Resources.Serach_1            Case 2                pb.Image = My.Resources.Serach_2            Case 3                pb.Image = My.Resources.Serach_3            Case Else                Debug.Print("Invalid")        End Select    End SubEnd Class
Link to comment
Share on other sites

Link to post
Share on other sites

Look over my code again. You're missing stuff.

do i need to use the if and select statement together because it seemed like you meant one or the other 

Link to comment
Share on other sites

Link to post
Share on other sites

Look over my code again. You're missing stuff.

nevermind this works with both now thank you 

Link to comment
Share on other sites

Link to post
Share on other sites

@madknight3 im now trying to make a label change its text depending on what image is in the picture box i tried doing this i get no errors but when i run it only the default text appears 

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click        If _imageId = 1 Then            Label1.Text = "Step 1:" & Environment.NewLine & "Left click the 'Select lifestyle' dropdown box and select a option"        End If    End Sub
Link to comment
Share on other sites

Link to post
Share on other sites

That looks like it should work to me.

 

With your code, if you click on the label and it's currently on image 1, then it should update the label text.

 

If you want the text to update at the same time the image updates, then you'll need to move it to the Button1_Click method.

Link to comment
Share on other sites

Link to post
Share on other sites

That looks like it should work to me.

 

With your code, if you click on the label and it's currently on image 1, then it should update the label text.

 

If you want the text to update at the same time the image updates, then you'll need to move it to the Button1_Click method.

so it works but only after i have gone through all 5 images once and have gotten back to the first heres my code i added a while loop so that the image changes after one press of the button not two

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click         While _imageId <= 0            _imageId = 1        End While        If _imageId = MaxId Then            _imageId = 1        Else            _imageId += 1        End If         SetImage(Steps, _imageId)         If _imageId = 1 Then            Label1.Text = "Step 1:" & Environment.NewLine & "Left click the 'Select Lifestyle' dropdown box" & Environment.NewLine & "and select a option"        End If    End Sub
Link to comment
Share on other sites

Link to post
Share on other sites

 

so it works but only after i have gone through all 5 images once and have gotten back to the first heres my code i added a while loop so that the image changes after one press of the button not two

 

That's because at the beginning the id is 0. Then the while loop is setting it to 1, and then the for loop is changing it to 2. So it's actually skipping 1 the first time.

 

You shouldn't need while loop at all.

 

Instead if you don't want to handle 0 or below, just to be safe, you could just add it as a condition to the if statement. I even added the >= check to MaxId for even more safety in case, somehow, the id skipped the MaxId.

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click         // Removed while loop and added another check to the if statement        If _imageId <= 0 OrElse _imageId >= MaxId Then            _imageId = 1        Else            _imageId += 1        End If         SetImage(Steps, _imageId)         If _imageId = 1 Then            Label1.Text = "Step 1:" & Environment.NewLine & "Left click the 'Select Lifestyle' dropdown box" & Environment.NewLine & "and select a option"        End If    End Sub
Link to comment
Share on other sites

Link to post
Share on other sites

 

That's because at the beginning the id is 0. Then the while loop is setting it to 1, and then the for loop is changing it to 2. So it's actually skipping 1 the first time.

 

You shouldn't need while loop at all.

 

Instead if you don't want to handle 0 or below, just to be safe, you could just add it as a condition to the if statement. I even added the >= check to MaxId for even more safety in case, somehow, the id skipped the MaxId.

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click         // Removed while loop and added another check to the if statement        If _imageId <= 0 OrElse _imageId >= MaxId Then            _imageId = 1        Else            _imageId += 1        End If         SetImage(Steps, _imageId)         If _imageId = 1 Then            Label1.Text = "Step 1:" & Environment.NewLine & "Left click the 'Select Lifestyle' dropdown box" & Environment.NewLine & "and select a option"        End If    End Sub

yeah but i have the first image seet as the default image if it is not then the picturebox will appear blank and after one press the first image will appear but if i use the loop the defalt image is present from first opening the form so that means i dont really need the case 1 option right? but the problem with that is that the label will not change as the _imageId is set to zero. if i use your if statement the picturebox will appear blank until the button is pressed for the first time and the label will show the correct text i just want this to be present when the form is first open before the first button press    

Link to comment
Share on other sites

Link to post
Share on other sites

yeah but i have the first image seet as the default image if it is not then the picturebox will appear blank and after one press the first image will appear but if i use the loop the defalt image is present from first opening the form so that means i dont really need the case 1 option right? but the problem with that is that the label will not change as the _imageId is set to zero. if i use your if statement the picturebox will appear blank until the button is pressed for the first time and the label will show the correct text i just want this to be present when the form is first open before the first button press    

 

Then I guess you'll want to set the _imageID and Label text in your form load event.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load    _imageId = 1    // These next lines don't need to be added if you've done this stuff in the designer    PictureBox1 = My.Resource.Serach_1    Label1.Text = "Initial text to display before button is clicked" End SubPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click    // Removed while loop and added another check to the if statement    If _imageId <= 0 OrElse _imageId >= MaxId Then        _imageId = 1    Else        _imageId += 1    End If    SetImage(Steps, _imageId)    // This is only needed if you're changing the label text since you'll want to reset it    If _imageId = 1 Then        Label1.Text = "Step 1:" & Environment.NewLine & "Left click the 'Select Lifestyle' dropdown box" & Environment.NewLine & "and select a option"    End IfEnd Sub
Link to comment
Share on other sites

Link to post
Share on other sites

 

Then I guess you'll want to set the _imageID and Label text in your form load event.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load    _imageId = 1    // These next lines don't need to be added if you've done this stuff in the designer    PictureBox1 = My.Resource.Serach_1    Label1.Text = "Initial text to display before button is clicked" End SubPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click    // Removed while loop and added another check to the if statement    If _imageId <= 0 OrElse _imageId >= MaxId Then        _imageId = 1    Else        _imageId += 1    End If    SetImage(Steps, _imageId)    // This is only needed if you're changing the label text since you'll want to reset it    If _imageId = 1 Then        Label1.Text = "Step 1:" & Environment.NewLine & "Left click the 'Select Lifestyle' dropdown box" & Environment.NewLine & "and select a option"    End IfEnd Sub

works great thanks im trying to get this print dialog box to work i used the PrintForm object it was working but has suddenly stopped im not sure if its because no priter is connected although it normally tries to print to OneNone if that is the case instead i get a runtime error  A first chance exception of type 'System.Drawing.Printing.InvalidPrinterException' occurred in System.Windows.Forms.dll is there anyway around this eg a message box etc also is there anyway to edit the PrintForms GUI because there is no designer window

Link to comment
Share on other sites

Link to post
Share on other sites

works great thanks im trying to get this print dialog box to work i used the PrintForm object it was working but has suddenly stopped im not sure if its because no priter is connected although it normally tries to print to OneNone if that is the case instead i get a runtime error  A first chance exception of type 'System.Drawing.Printing.InvalidPrinterException' occurred in System.Windows.Forms.dll is there anyway around this eg a message box etc also is there anyway to edit the PrintForms GUI because there is no designer window

I've never used PrintForm before so I'm not sure.

 

If you post your code maybe I or someone else can help you out. Probably best to start a new topic for this question though.

Link to comment
Share on other sites

Link to post
Share on other sites

I've never used PrintForm before so I'm not sure.

 

If you post your code maybe I or someone else can help you out. Probably best to start a new topic for this question though.

would there be a way to make just a regular form that does the same as the PrintForm but i cange change the GUI if you dont know ill start a new thread 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×