C
C#2mo ago
Silme94

Windows Forms drawing not saving

When im drawing on the picturebox (pageImage) and im saving, the data is not saved
No description
39 Replies
Silme94
Silme94OP2mo ago
how i create Graphics instance
g = pageImage.CreateGraphics();
g = pageImage.CreateGraphics();
private bool isDrawing = false;
private Point pos;
private Pen drawingPen;


private void PageImage_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDrawing = true;
pos.X = e.X;
pos.Y = e.Y;
}
}

private void PageImage_MouseDrawMove(object sender, MouseEventArgs e)
{
if (isDrawing)
{
g.DrawLine(drawingPen, pos, e.Location);

pos = e.Location;
}
}

private void PageImage_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDrawing = false;


Bitmap bitmap = new Bitmap(pageImage.Image);
bitmap.Save(pageImage.ImageLocation);
}
}
private bool isDrawing = false;
private Point pos;
private Pen drawingPen;


private void PageImage_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDrawing = true;
pos.X = e.X;
pos.Y = e.Y;
}
}

private void PageImage_MouseDrawMove(object sender, MouseEventArgs e)
{
if (isDrawing)
{
g.DrawLine(drawingPen, pos, e.Location);

pos = e.Location;
}
}

private void PageImage_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDrawing = false;


Bitmap bitmap = new Bitmap(pageImage.Image);
bitmap.Save(pageImage.ImageLocation);
}
}
look at PageImage_MouseUp event im able to draw on the window, but when it comes to save, it does not work
MonJamp
MonJamp2mo ago
Does isDrawing ever get set to false? nvm Can you clarify "does not work"? Does a bitmap image at least get created?
Silme94
Silme94OP2mo ago
the image does not change, it keep original and the drawing does not affect like on the picturebox i see that i drew but when it comes to save it to a file the changes dont applies
MonJamp
MonJamp2mo ago
When you are drawing, I believe the drawing data gets saved to the g object. When you save, it looks like you're just saving the page but that doesn't contain the data which you drew Maybe I'm wrong?
Silme94
Silme94OP2mo ago
i think so, but then how can i save the Graphics too?
MonJamp
MonJamp2mo ago
Are you able to convert g to a bitmap or image object?
Silme94
Silme94OP2mo ago
uh i think
MonJamp
MonJamp2mo ago
Check out the documentation, I'm sure one of the methods will help you out here The second answer in the stackoverflow page sounds promising: Bitmap bmp = new Bitmap(100,100,graphics);
Silme94
Silme94OP2mo ago
@MonJamp it just save a blank page Bitmap bitmap = new Bitmap(pageImage.Width, pageImage.Height, g); without the drawing man idk thank you for trying to help
MonJamp
MonJamp2mo ago
For sure There are many different ways you could go about to implement this, I'm not too familiar with how to use the Graphics object but maybe the best approach would be to save the pixel data in an array as you're drawing - just an idea Good luck
Nasdack
Nasdack2mo ago
You're saving pageImage not its Graphics created instance called g
MonJamp
MonJamp2mo ago
Ye that's what we figured out before, the difficulty is saving the Graphics object to a bitmap And combining both the pageImage and g
Silme94
Silme94OP2mo ago
then how can i combine these 2 instance to save them in one file?
Nasdack
Nasdack2mo ago
Try this Create a new Bitmap that has your image new Bitmap(pageImage.Image) Create the Graphics from this bitmap Graphics.FromImage(bitmap) Set pageImage.Image = bitmap Draw as you normally would, but call pageImage.Invalidate() at the end so it can display changes Then on save you want to bitmap.Save(pageImage.ImageLocation, ...)
Silme94
Silme94OP2mo ago
@Nasdack "A generic error occurred in GDI+" when the mouseUp event if fired :
drawingBitmap = new Bitmap(pageImage.ImageLocation);
//g = pageImage.CreateGraphics();
g = Graphics.FromImage(drawingBitmap);
pageImage.Image = drawingBitmap;
drawingBitmap = new Bitmap(pageImage.ImageLocation);
//g = pageImage.CreateGraphics();
g = Graphics.FromImage(drawingBitmap);
pageImage.Image = drawingBitmap;
private void PageImage_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDrawing = true;
pos.X = e.X;
pos.Y = e.Y;
}
}

private void PageImage_MouseDrawMove(object sender, MouseEventArgs e)
{
if (isDrawing)
{
g.DrawLine(drawingPen, pos, e.Location);

pos = e.Location;

pageImage.Invalidate();
}
}

private void PageImage_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDrawing = false;

drawingBitmap.Save(pageImage.ImageLocation);
}
}
private void PageImage_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDrawing = true;
pos.X = e.X;
pos.Y = e.Y;
}
}

private void PageImage_MouseDrawMove(object sender, MouseEventArgs e)
{
if (isDrawing)
{
g.DrawLine(drawingPen, pos, e.Location);

pos = e.Location;

pageImage.Invalidate();
}
}

private void PageImage_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDrawing = false;

drawingBitmap.Save(pageImage.ImageLocation);
}
}
when it tries to save the image
Nasdack
Nasdack2mo ago
drawingBitmap = new Bitmap(pageImage.ImageLocation); can that be .Image instead
Silme94
Silme94OP2mo ago
i dont think so cause im setting the with with ImageLocation
Nasdack
Nasdack2mo ago
this error happens when the file is in use try to save to a different location
Silme94
Silme94OP2mo ago
already tried
Nasdack
Nasdack2mo ago
using g = ... this would dispose it but that's not what we want
Silme94
Silme94OP2mo ago
im not using "using" ?
Nasdack
Nasdack2mo ago
pageImage.Image?.Dispose(); try this before saving
Silme94
Silme94OP2mo ago
same error and its not even drawing anything in the two case
Nasdack
Nasdack2mo ago
if you draw nothing happens?
Silme94
Silme94OP2mo ago
when i move the cursor its not drawingbut when i up the mouse it says the error the lines are not drawing but i think its bc of the errors u know
Nasdack
Nasdack2mo ago
I found this and if you follow it through and rewrite your code it should be errorless
Silme94
Silme94OP2mo ago
i dont see nothing wrong
Silme94
Silme94OP2mo ago
Silme94
Silme94OP2mo ago
this is full code, read the class constructor and the end of the code @Nasdack do you see anything wrong ?
Nasdack
Nasdack2mo ago
the error has to do with saving the file nothing else make sure you're saving to another location
Silme94
Silme94OP2mo ago
nah not the issue
private void PageImage_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDrawing = false;

//MemoryStream ms = new MemoryStream();
pageImage.Image?.Dispose();
drawingBitmap.Save("C:\\test.png"); //pageImage.ImageLocation);


}
}
private void PageImage_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDrawing = false;

//MemoryStream ms = new MemoryStream();
pageImage.Image?.Dispose();
drawingBitmap.Save("C:\\test.png"); //pageImage.ImageLocation);


}
}
Nasdack
Nasdack2mo ago
what's the issue send a screenshot with details
Silme94
Silme94OP2mo ago
ok
Silme94
Silme94OP2mo ago
@Nasdack
No description
Nasdack
Nasdack2mo ago
Scrap this codebase It's a mess Just follow the tutorial here

Did you find this page helpful?