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();
}
}
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?
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'?