C# help

Started by
14 comments, last by namingway 16 years, 1 month ago
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?
Advertisement
Quote:Original post by namingway
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?


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.
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.
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.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

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.
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.
use the Width and Height
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.
Check the value returned by ofd.ShowDialog().

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

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.

This topic is closed to new replies.

Advertisement