Public Group

# C# help

This topic is 3735 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a menu item which I want to load an image from file into a picture box. Also is it possible to add scrollbars to picture boxes or will I need a different component?

##### Share on other sites
Quote:
 Original post by namingwayI have a menu item which I want to load an image from file into a picture box. Also is it possible to add scrollbars to picture boxes or will I need a different component?

I don't understand your first question. About the second one, you can add the picturebox into a panel with autoscroll enabled. That should make scrollbars appear as soon as the picture becomes larger than the containing panel...

EDIT: if you're asking how to ask the user for a file, you need to hasndle the event fired when a click on the menu item is performed, then you must create and show an OpenFileDialog and get the selected file. With the path you can then create a Bitmap with the name as the parameter of the constructor.

##### Share on other sites
ah my apologies, I'd like some help on how to setup an openfile dialog then assign the chosen file the picture box's image.

##### Share on other sites
Your language is a little unclear. Do you want to:

1) Load the image from the file and display it in the picturebox
2) Save the image from the picturebox to the file

For both options, you begin by hooking up an event to the menu item. In the code for that event, you then create an OpenFileDialog or a SaveFileDialog, use it to get the filename, and then either create a bitmap from that file and set it into the picturebox, or get the image from the picturebox and save it to that file.

You're going to have to ask more specific questions if you want more specific help.

##### Share on other sites
Again sorry, I've been in a rush. At this point in time all I'd like to know is how to store the image e.g. .bmp, .png chosen from an open file dialog file and then assign it to a picture box.

##### Share on other sites
oh nevermind got it.

A new question, how can I set a limit on the dimensions of the loaded image, say if the image isnt 300x800 then it won't load.

##### Share on other sites
use the Width and Height

##### Share on other sites
simple enough, okay now I have this so far; ofd = openFileDialog ;)
private void loadTilesetToolStripMenuItem_Click(object sender, EventArgs e)        {            ofd.ShowDialog();            _tileset = new Bitmap(ofd.FileName);            if (_tileset.Width == 256 && _tileset.Width == 640)                picTileset.Image = _tileset;            else            {                TilesetError err = new TilesetError();                err.ShowDialog();            }           }

Problem is if the user clicks cancel it brings up the error message as well as if the dimensions arent met, how can I fix it so that if the user cancels the openfile dialog no error will show up?

Thanks for the help so far it is much appreciated.

##### Share on other sites
Check the value returned by ofd.ShowDialog().

##### Share on other sites
cheers, I have yet another question...
I have dialog for when the user clicks File -> new , this dialog takes in 3 values, how can I transfer the values over to the main form?
I need numHeight, numWidth to be set to the mainForms _columns and _rows properties.

• 12
• 9
• 13
• 41
• 15