saveFileDialog error--with Image/BitMap files--what am I doing wrong?

R

raylopez99

I can't get the below code to work, no matter how many times I change
it. I do have the Image file, Image1, created (in another method),
and using "hard coding" I can save it (it's a filled rectangle) on my
hard drive, but I cannot use the Dialog box to select a user defined
name. I keep getting the error "Wrong Parameter Selected" no matter
what name I pick. Also I got rid of the 'filters' for extensions and
it still doesn't work. saveFileDialog1 was created with the wizard.

It's something very obvious I'm sure, but I just can't figure it out
(this is my first time saving a binary file alone; for some reason I
can binary serialize and use StreamWriter/TextWriter, but with this
Image file in this saveFileDialog, nothing works).

Any ideas?

RL

Stream myStream;

if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if((myStream = saveFileDialog1.OpenFile()) != null)
{
// Code to write the stream goes here.

string path0 =
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

string filename01 = saveFileDialog1.FileName;

// string fullpathAndFilename = path0 +"\\"+
filename01; //did not work
// string fullpathAndFilename = path0 + filename01; //also did not
work

// // Image1.Save(fullpathAndFilename,
system.Drawing.Imaging.ImageFormat.Png);//did not work

// Image1.Save(filename01); //does not work

//Image1.Save(fullpathAndFilename); //does not work


//Image1.Save(@"C:\image33.png"); //this works, but I
want a non-hard code way of entering the name

myStream.Close();
}
}
 
R

raylopez99

Update: I believe it's not a saveFileDialog error so much as an Image
file format error! if I comment out all image files, the dialog
'works' in the sense that no exception is thrown.

I know I have the Image file created in another method (and saved to
my harddrive, since I can view it after the program exits), and it's
'global' to the Form, so it should be ready to be saved in this
FileDialog, but, perhaps I have to 'wrap' it with Graphics handler of
some sort, and reconstruct it again in this method, along the lines
of:

Void MySaveImageFileMethod (object sender, EventArgs e)
{

using (Graphics myGraphics = this.CreateGraphics())

then

using (Image myImage1Again = new Bitmap(10,10,displayGraphics); //
width and H = 10 pixels

then

do the saveFileDialog.

Anybody agree with this? Or is my saveFileDialog at fault?
 
R

raylopez99

I found the solution. It was indeed the pecularities of Image and
Bitmap (which is a subset of Image). It's very bizarre. I assumed
Bitmap and Image are like 'text files' except binary, but in fact you
have to 'wrap them' in a graphics object. See the below complete
code, which works, on a form of screen capture.

Adapted from Chris Sells Chapter 5 "Windows Forms 2.0 Programming"

//declare this member variable before class Form instantiation as a
member variable (that is, outside the class that has
InitializeComponent();), say just after your second bracket after the
namespace bracket "{" will do, after ' public partial class MyForm:
Form'. Don't initialize, just declare it: Bitmap myBitmapScreenShot;

//click on this button to capture the screen.

private void ScreenCapture_button (object sender, EventArgs e)
{
Rectangle screenRect = Screen.PrimaryScreen.WorkingArea;
myBitmapScreenShot = new Bitmap (screenRect.Width, screenRect.Height);
using (Graphics targetGraphics = Graphics.FromImage
(myBitmapScreenShot))
{
targetGraphics.CopyFromScreen(0,0,0,0,new
Size(myBitmapScreenShot.Width,myBitmapScreenShot.Height));

}

// now click on this button to save the screenshot to file

private void MySAVEButton_Click(object sender, EventArgs e)
{

String path;
Stream myStream;
saveFileDialog1.InitialDirectory = "C:\
\imageScreenSave.png";
saveFileDialog1.Filter = "picture files (*.png)|*.png";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;

using (Graphics displayGraphics = this.CreateGraphics())
//create Bitmap to draw into based on existing Graphics
object (p. 222 Sells)

using (Image image = new Bitmap(myBitmapScreenShot))

//wrap Graphics object around image to draw into

using (Graphics imageGraphics = Graphics.FromImage(image))
{

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{

if ((myStream = saveFileDialog1.OpenFile()) !=
null)
{

path = saveFileDialog1.FileName;
//image.Save(path);//works not at all- why?
Who cares!
image.Save(myStream,
System.Drawing.Imaging.ImageFormat.Png); //works fine
myStream.Close(); //is this redundant since
wrapped in a 'using'?
}
}
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top