none
from Resource to PictureBox

    Question

  • hi,

     

    I'm trying to load a bitmap from the resources to a pictureBox but the 'load' function requires an url of type string ...

     

    PictureBox[] picCards = new PictureBox[52];

    private void btnDisplayCards_Click(object sender, EventArgs e)

    {

    picCards[1] = new PictureBox( );

    picCards[1].Load(Crazy_Gin.Properties.Resources.H01);  <- this doesn't work

    }

     

    any ideas

    BadButBit

    Monday, November 19, 2007 11:42 PM

Answers

  • I think the issue you're running into is with the way you've tried embedding the resources. It looks like you're trying to embed the images within the default project resource file. If you want to select from resources easily in your program, you're probably better off creating them within their own resource file and using the class the designer creates for you.

    In your project add a new item and select "Resource File". Give it a name something like "CardImages.resx" This will automatically create an empty resource file along with a designer that will include code for accessing the resources you'll add.

    In the new resource file use the "Add Resource" button at the top of the designer and select "From File", add each of your bitmaps, then give them a proper label. (I.e. "H01")

    Now from code when you want to load a bitmap, you just need to use:
    picCards[0].Image = CardImages.H01


    A couple other things you might be getting caught up on:
    1) Arrays start with element 0, not 1.
    2) Your code doesn't add the picture box to the form, you need to add the picturebox to the form's Controls collection before the picturebox will appear on screen. The best easy option for laying out cards on the screen is to put a FlowLayout control on the form and size it to the area you want to display cards, then use:
    PictureBox uxCardPicture = new PictureBox();
    uxCardPicture.Image = CardImages.H01;
    this.uxFlowLayout.Controls.Add(uxCardPicture); // where uxFlowLayout is the flow layout control placed on the form.



    Tuesday, November 20, 2007 1:00 AM
  • Hi,

     

    Please try   picCards[1].Image = Crazy_Gin.Properties.Resources.H01;

    instead of using the Load method as the load method expects a url / path to an image and images in resources don't qualify for that.

     

    cheers

    Sharad

     

     

     

    Tuesday, November 20, 2007 1:14 AM

All replies

  • I think the issue you're running into is with the way you've tried embedding the resources. It looks like you're trying to embed the images within the default project resource file. If you want to select from resources easily in your program, you're probably better off creating them within their own resource file and using the class the designer creates for you.

    In your project add a new item and select "Resource File". Give it a name something like "CardImages.resx" This will automatically create an empty resource file along with a designer that will include code for accessing the resources you'll add.

    In the new resource file use the "Add Resource" button at the top of the designer and select "From File", add each of your bitmaps, then give them a proper label. (I.e. "H01")

    Now from code when you want to load a bitmap, you just need to use:
    picCards[0].Image = CardImages.H01


    A couple other things you might be getting caught up on:
    1) Arrays start with element 0, not 1.
    2) Your code doesn't add the picture box to the form, you need to add the picturebox to the form's Controls collection before the picturebox will appear on screen. The best easy option for laying out cards on the screen is to put a FlowLayout control on the form and size it to the area you want to display cards, then use:
    PictureBox uxCardPicture = new PictureBox();
    uxCardPicture.Image = CardImages.H01;
    this.uxFlowLayout.Controls.Add(uxCardPicture); // where uxFlowLayout is the flow layout control placed on the form.



    Tuesday, November 20, 2007 1:00 AM
  • Hi,

     

    Please try   picCards[1].Image = Crazy_Gin.Properties.Resources.H01;

    instead of using the Load method as the load method expects a url / path to an image and images in resources don't qualify for that.

     

    cheers

    Sharad

     

     

     

    Tuesday, November 20, 2007 1:14 AM
  • Thanks for the help,

    I'm not sure what the -uxFlowLayout control- does... can you elaborate?
    BadButBit

    Tuesday, November 20, 2007 1:41 AM
  • Do you want to know about the FlowLayoutPanel?

     

    Tuesday, November 20, 2007 2:13 AM
  • The FlowLayoutPanel and TableLayoutPanel are controls that helps arrange the controls in a desirable manner. The FlowLayoutPanel would arrange controls from left to right, right to left, top down, bottom up. The alignment is handled automatically by the Panel. You can use the Panel's FlowDirection property to set how the controls should be arranged on the panel. The TableLayoutPanel gives a tabular arrangement to the controls.

     

    cheers

    Sharad

    Tuesday, November 20, 2007 2:25 AM
  •  

    hi,

    always glad to have a new tool in my toolbox but is a flowcontrol necessary?  how much use is this since I can set these(cards in this case) pictures manually with loops.

    BadButBit

    Tuesday, November 20, 2007 2:00 PM