Changing image in PictureBox

Depending on the image already there.

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 39197 Views - Last Post: 07 August 2008 - 05:06 PM Rate Topic: -----

#1 Mommy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 137
  • Joined: 04-August 08

Changing image in PictureBox

Posted 07 August 2008 - 03:59 AM

EDIT: PLEASE READ AT THE BOTTOM :).


Hey guys. I am trying to make an application that's supposed to be used as a "map viewer" for a 2D game. I took screen shots from the 2D game, and put them in the resources of the project.

The GUI looks like this :

Posted Image

You can see all the maps of the 2D game here:
http://borelli-kjaer...melig/maps.html


The radiobutton is selected on startup, and when it is active, this code runs:
        mapBox.Image = My.Resources.kalm2
        mapTxt.Text = "Kalm Inn and Bank"



That works fine. The picturebox changes the image to kalm2 and the label changes to "Kalm Inn and Bank" which are the houses on the map (Kalm is the village name ^.^).

I have the 4 buttons that I want to use as navigation (north, south, east, west)
Obviously if I just set the map to change to a specific map, when clicking one of those, it would be messed up, cause they have to change map, depending on which map they're on.

I started out by adding code to when north is clicked:
mapBox.Image = My.Resources.kalm1
mapTxt.Text = "North Entrance Kalm"
east.hide
north.hide
west.hide



This worked, the map changed to kalm1 and all the navigation buttons, except south were hidden.
Now to the problem: The problem is that, I don't know how to change what happens when clicking south, I don't even know if I'm supposed to put all this code in under the event of clicking north.
I think I tried something like adding
If south_Click then
mapBox.image = My.Resources.kalm1
mapTxt.Text = "Kalm Inn and Bank"


This did not work.. I have been thinking about this for a long time, but I just can't come up with a solution =/.

Please help me :)..

Thanks,
Mads.

This post has been edited by Mommy: 07 August 2008 - 07:00 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Changing image in PictureBox

#2 dineeshd  Icon User is offline

  • member icon

Reputation: 38
  • View blog
  • Posts: 619
  • Joined: 30-June 08

Re: Changing image in PictureBox

Posted 07 August 2008 - 04:21 AM

This all seems to be little confusing for me... :blink: I need some inputs from you for better understanding of the scenario. What you are expecting to happen when SOUTH is clicked?
Was This Post Helpful? 0
  • +
  • -

#3 Mommy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 137
  • Joined: 04-August 08

Re: Changing image in PictureBox

Posted 07 August 2008 - 04:26 AM

When south is clicked it changes to another map, depending on which one it's already on.

For example: if the picturebox is map3 and I click south, it changes to map2, and when I click south on map2 it changes to map1.
Was This Post Helpful? 0
  • +
  • -

#4 Mommy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 137
  • Joined: 04-August 08

Re: Changing image in PictureBox

Posted 07 August 2008 - 07:07 AM

I almost sorted it out (I think).

I added this:

    Private locNS As Integer
    Private locEW As Integer


and:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        rbtnKalm.Select()
        locNS = 10
        locEW = 10
    End Sub

    Private Sub rbtnKalm_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtnKalm.CheckedChanged
        mapBox.Image = My.Resources.kalm_(locNS)_(locEW)
    End Sub

    Private Sub north_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles north.Click
        locNS = locNS + 1
    End Sub

    Private Sub south_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles south.Click
        locNS = locNS - 1
    End Sub

    Private Sub west_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles west.Click
        locEW = locEW - 1
    End Sub

    Private Sub east_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles east.Click
        locEW = locEW + 1
    End Sub



Changed the names of all the maps to start with kalm_
The first map is now called kalm_10_10 (think of it like a index system or something.. the first 10 is the x value and the second 10 is the y value)
When you click north for example, the image in the picturebox should change to kalm_11_10 (it adds 1 to locNS).

The problem is that it gives me this error with mapBox.Image = My.Resources.kalm_(locNS)_(locEW):
Error 1 Class 'System.Drawing.Bitmap' cannot be indexed because it has no default property.
and
Error 2 Identifier expected.

Could anyone solve this for me ? It's probably really simple, I just don't know how :P.
Was This Post Helpful? 0
  • +
  • -

#5 Bort  Icon User is offline

  • Ill-informed Mongoloid
  • member icon

Reputation: 445
  • View blog
  • Posts: 3,084
  • Joined: 18-September 06

Re: Changing image in PictureBox

Posted 07 August 2008 - 07:16 AM

Dim ImageLoc as String
ImageLoc = "My.Resources.kalm_(" & locNS & ")_(" & locEW & ")"

Then have:

mapBox.Image = ImageLoc & ".jpg" 'Or whatever file extension you have on your map images.

That should about do it I think. Maybe create a new method that updates the string as people click on the buttons.
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Changing image in PictureBox

Posted 07 August 2008 - 07:19 AM

    Private Sub rbtnKalm_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtnKalm.CheckedChanged
        mapBox.Image = My.Resources.kalm_(locNS)_(locEW)
    End Sub



It is treating the line as if it were an array, remove them. Rename the resources to match.
Suggested Format: kalm_Y01_X01

This post has been edited by AdamSpeight2008: 07 August 2008 - 07:22 AM

Was This Post Helpful? 0
  • +
  • -

#7 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Changing image in PictureBox

Posted 07 August 2008 - 07:47 AM

I've noticed that you also need to change the size of the Image once you modify it for the change to take effect:
        pBox.Image = imageName
        pBox.Size = imageName.Size

Was This Post Helpful? 0
  • +
  • -

#8 Mommy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 137
  • Joined: 04-August 08

Re: Changing image in PictureBox

Posted 07 August 2008 - 10:54 AM

Okay I'm seriously lost, this is the relevant code that I have

Public Class Form1

    Private locNS As Integer
    Private locEW As Integer
    Private IsFormBeingDragged As Boolean = False
    Private MouseDownX As Integer
    Private MouseDownY As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        rbtnKalm.Select()
        locNS = 10
        locEW = 10
    End Sub
    Private Sub rbtnKalm_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtnKalm.CheckedChanged
        Dim imageLoc As String
        imageLoc = "My.Resources.kalm_(" & locNS & ")_(" & locEW & ")"
        mapBox.Image = imageLoc & ".png"
    End Sub
    Private Sub north_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles north.Click
        locNS = locNS + 1
    End Sub

    Private Sub south_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles south.Click
        locNS = locNS - 1
    End Sub

    Private Sub west_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles west.Click
        locEW = locEW - 1
    End Sub

    Private Sub east_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles east.Click
        locEW = locEW + 1
    End Sub
End Class


It gives me error: Error 1 Value of type 'String' cannot be converted to 'System.Drawing.Image'.
I don't understand what to do, sorry =s. Adam I didn't understand what you said either.
Was This Post Helpful? 0
  • +
  • -

#9 PsychoCoder  Icon User is offline

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

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

Re: Changing image in PictureBox

Posted 07 August 2008 - 11:07 AM

The PictureBox is expecting an image, you're providing a string. Try this


Private Sub rbtnKalm_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtnKalm.CheckedChanged
        Dim imageLoc As String
        imageLoc = "My.Resources.kalm_(" & locNS & ")_(" & locEW & ")"
        mapBox.Image = System.Drawing.Image.FromFile(imageLoc & ".png")
    End Sub



And see if that resolves the error you're receiving :)
Was This Post Helpful? 0
  • +
  • -

#10 Mommy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 137
  • Joined: 04-August 08

Re: Changing image in PictureBox

Posted 07 August 2008 - 12:23 PM

Thanks for your last reply, PhychoCoder :). I am getting close to something working now ;). The only error now is that it can't find it (I've doubled checked files).

Gives this error: A first chance exception of type 'System.IO.FileNotFoundException' occurred in System.Drawing.dll

Says it can't find My.Resources.kalmx10y10.png

The code looks like this :

    Private Sub rbtnKalm_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtnKalm.CheckedChanged
        Dim imageLoc As String
        locNS = 10
        locEW = 10
        imageLoc = "My.Resources.kalm" & "x" & locNS & "y" & locEW
        mapBox.Image = System.Drawing.Image.FromFile(imageLoc & ".png")
    End Sub



As you can see on the following image, all resources are there.

Posted Image

Thanks for all your help, guys :).
Was This Post Helpful? 0
  • +
  • -

#11 PsychoCoder  Icon User is offline

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

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

Re: Changing image in PictureBox

Posted 07 August 2008 - 12:44 PM

You're getting that error because the file you're looking for doesnt exist. When retrieving items from an embedded resource you have to preface it with the assembly name. Try this


Private Function GetImage(ByRef name As String) As Image
    'Create a reference to the assembly for getting the name of the executing assembly
    Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()

    'Get the image from the resource file
    Dim imgName As String = assem.GetManifestResourceStream(assem.GetName().ToString() + ".Resources." + name)

    'Create an image from the file name
    Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(imgName)

    'Return the image
    Return img
End Function

Private Sub rbtnKalm_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtnKalm.CheckedChanged
        Dim imageLoc As String
        locNS = 10
        locEW = 10

        mapBox.Image = GetImage("kalmx" & locNS & "y" & locEW & ".png")
End Sub


Was This Post Helpful? 1
  • +
  • -

#12 dineeshd  Icon User is offline

  • member icon

Reputation: 38
  • View blog
  • Posts: 619
  • Joined: 30-June 08

Re: Changing image in PictureBox

Posted 07 August 2008 - 01:32 PM

@PsychoCoder

Sorry If I am wrong.

 Dim imgName As String = assem.GetManifestResourceStream(assem.GetName().ToString() + ".Resources." + name)


This should give an error. Out put of assem.GetManifestResourceStream is System.IO.Stream type and imgName is string. So Conversion error can occur.
Was This Post Helpful? 0
  • +
  • -

#13 Mommy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 137
  • Joined: 04-August 08

Re: Changing image in PictureBox

Posted 07 August 2008 - 01:33 PM

Hey PsychoCoder, thanks again for the reply :).
Now it returns me this error.
Error 1 Value of type 'System.IO.Stream' cannot be converted to 'String'

I'll promise to look at a tutorial of more advanced vb.net programming that I've seen so far, so I can fix these errors without asking for your help all the time :P. Sorry :P

Thanks.
Was This Post Helpful? 0
  • +
  • -

#14 dineeshd  Icon User is offline

  • member icon

Reputation: 38
  • View blog
  • Posts: 619
  • Joined: 30-June 08

Re: Changing image in PictureBox

Posted 07 August 2008 - 01:41 PM

Make this change...

Dim imgName As String = assem.GetManifestResourceStream(assem.GetName().ToString() + ".Resources." + name).ToString

Was This Post Helpful? 0
  • +
  • -

#15 Mommy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 137
  • Joined: 04-August 08

Re: Changing image in PictureBox

Posted 07 August 2008 - 01:57 PM

Hmm this ain't going so well :P .. Perhaps someone could try and download the project file (doing it in VS 2008 Express).
It gives me this error now:
A first chance exception of type 'System.IO.FileNotFoundException' occurred in System.Drawing.dll
A first chance exception of type 'System.IO.FileNotFoundException' occurred in System.Drawing.dll
A first chance exception of type 'System.NullReferenceException' occurred in mapviewer.exe
But this only happens when I run it.
It opens some sort of window that says :
NullReferenceException was unhandled
Object reference not set to an instance of an object.

And it points at the
Dim imgName As String = assem.GetManifestResourceStream(assem.GetName().ToString() + ".Resources." + name).ToString

line...

Download the project here:
http://www.borelli-k...g/mapviewer.rar

Thanks guys.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2