C
C#4d ago
Silme94

Windows Forms drawing not saving

When im drawing on the picturebox (pageImage) and im saving, the data is not saved
No description
12 Replies
Silme94
Silme94OP4d 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
MonJamp4d ago
Does isDrawing ever get set to false? nvm Can you clarify "does not work"? Does a bitmap image at least get created?
Silme94
Silme94OP4d 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
MonJamp4d 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
Silme94OP4d ago
i think so, but then how can i save the Graphics too?
MonJamp
MonJamp4d ago
Are you able to convert g to a bitmap or image object?
Silme94
Silme94OP4d ago
uh i think
MonJamp
MonJamp4d 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
Silme94OP4d 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
MonJamp4d 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
Want results from more Discord servers?
Add your server