Save Drawn Images PictureBox [duplicate] - c#

This question already has answers here:
How to save graphics created on a PictureBox?
(2 answers)
Closed 2 years ago.
My program allows the user to draw in a PictureBox.
I'm trying to save the pictureBox1 as a .jpg file but this file is empty.
My save button:
Bitmap bm = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);
this.pictureBox1.DrawToBitmap(bm, this.pictureBox1.ClientRectangle);
bm.Save(String.Format("{0}.jpg", this.ID));
this.pictureBox1.CreateGraphics().Clear(Color.White);
My draw event:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
drawNote.isDraw = true;
drawNote.X = e.X;
drawNote.Y = e.Y;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if(drawNote.isDraw)
{
Graphics G = pictureBox1.CreateGraphics();
G.DrawLine(drawNote.pen, drawNote.X, drawNote.Y, e.X, e.Y);
drawNote.X = e.X;
drawNote.Y = e.Y;
}
}

You should create an empty Bimap set pictureBox1.Image by this bitmap then creat graphics from it, also you must store it in global variable to prevent recreat it.
Like this:
Graphics graphics = null;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if(drawNote.isDraw)
{
if (graphics == null)
{
graphics = pictureBox1.CreateGraphics();
Bitmap bmp = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);
pictureBox1.Image = bmp;
graphics = Graphics.FromImage(bmp);
graphics.Clear(Color.White);
}
graphics.DrawLine(drawNote.pen, drawNote.X, drawNote.Y, e.X, e.Y);
graphics.Flush();
graphics.Save();
pictureBox1.Refresh();
drawNote.X = e.X;
drawNote.Y = e.Y;
}
}
And you could do this by this simple code:
using (FileStream fileStream = new FileStream(#"C:\test.jpg", FileMode.Create))
{
pictureBox1.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}

Related

How can I save a graphics object to a bitmap in a "paint" winforms application?

My problem is:
When I try to save the graphics object to a bitmap image it does not save correctly, instead the image is black in color and there is nothing else in the file.
I've seen other answers, but I think it's different when you draw multiple times in the graphics object.
So, here's my attempt, please let me know where my issue is.
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace PenFlip
{
public partial class Match : Form
{
Graphics g;
private int x = -1;
private int y = -1;
private bool moving;
private Pen pen;
private Bitmap testBmp;
public Match()
{
InitializeComponent();
g = panel1.CreateGraphics();
g.SmoothingMode = SmoothingMode.AntiAlias;
pen = new Pen(Color.Black, 5);
pen.StartCap = pen.EndCap = LineCap.Round;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
PictureBox pictureBox = (PictureBox) sender;
pen.Color = pictureBox.BackColor;
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
moving = true;
x = e.X;
y = e.Y;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (moving && x != -1 && y != -1)
{
g.DrawLine(pen, new Point(x, y), e.Location);
x = e.X;
y = e.Y;
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
moving = false;
x = -1;
y = -1;
g.Save();
}
private void button1_Click(object sender, EventArgs e)
{
// prints out the black image
testBmp = new Bitmap(400, 200, g);
testBmp.Save(#"test.bmp", ImageFormat.Bmp);
}
}
}
Uh, usage of Bitmap is tricky and has a lot of pitfalls (that's why it's use is deprecated, btw). Try to create the Graphics object from an in-memory bitmap, instead of grabbing it from a control. The bitmap should be the primary object, not the Graphics instance. You don't know what the control does to your bitmap.
So in the constructor, do something like:
public Match()
{
InitializeComponent();
bitmap = new Bitmap(panel1.Width, panel1.Height, PixelFormat.Format32bppArgb);
pen = new Pen(Color.Black, 5);
pen.StartCap = pen.EndCap = LineCap.Round;
}
and in each function, create a graphics object from the bitmap:
using (var graphics = Graphics.FromImage(image))
{
graphics.Draw(...);
}
You need to update the panel manually now, e.g. by attaching to it's OnPaint event.

How to transfer a zoomed image from a pictureBox to a second pictureBox? .NET Framework 4.7

I have 2 pictureBoxes on my Form. In the right pictureBox I can zoom and pan an image. The illustration on the left shows only the image. Unfortunately I can‘t copy the zoomed image from pictureBox1(right) into pictureBox2. How should i do that? I’m thankful for any hint!
public Form1()
{
InitializeComponent();
string imgFile = #"C:\Users\Pictures\Clock.jpg";
img = Image.FromFile(imgFile);
Graphics g = this.CreateGraphics();
zoom = ((float)pictureBox1.Width / (float)img.Width) * (img.HorizontalResolution / g.DpiX);
pictureBox1.Paint += new PaintEventHandler(ImageBox_Paint);
}
private void ImageBox_Paint(object sender, PaintEventArgs e)
{
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
e.Graphics.ScaleTransform(zoom, zoom);
e.Graphics.DrawImage(img, imgX, imgY);
}
private void Button1_Click(object sender, EventArgs e)
{
Bitmap newbmp= new Bitmap(pictureBox2.ClientSize.Width,pictureBox2.ClientSize.Height);
using (Bitmap oldbmp = new Bitmap(img, pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height))
{
using (Graphics g = Graphics.FromImage(newbmp))
{
g.DrawImage(oldbmp, new Rectangle(0, 0,
pictureBox2.ClientSize.Width,pictureBox2.ClientSize.Height));
}
}
pictureBox2.Image = newbmp;
}

I can not find the error in the code.a picture Box does not update

I'm creating a software in which pictureBox2 updates with the color of the pixel clicked on pictureBox1.
Already tried to use refresh(), but pictureBox2 does not change.
private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
{
Bitmap b = new Bitmap(pictureBox1.Image);
color = b.GetPixel(e.X, e.Y); // Color
solidColor = new Bitmap(pictureBox2.Width, pictureBox2.Height, PixelFormat.Format24bppRgb); //Image
using (Graphics grp = Graphics.FromImage(solidColor))
{
SolidBrush co = new SolidBrush(color);
grp.FillRectangle( co, 0, 0, pictureBox2.Width, pictureBox2.Height);
}
pictureBox2.Image = solidColor;
}
I was able to solve this by doing this.
private void PictureBox1_Click(object sender, EventArgs e)
{
Bitmap b = new Bitmap(pictureBox1.Image);
MouseEventArgs me = (MouseEventArgs)e;
Point cord= me.Location;
color = b.GetPixel(cord.X,cord.Y);
solidColor = new Bitmap(pictureBox2.Width, pictureBox2.Height, PixelFormat.Format24bppRgb);
using (Graphics grp = Graphics.FromImage(solidColor))
{
SolidBrush co = new SolidBrush(color);
grp.FillRectangle(co, 0, 0, pictureBox2.Width, pictureBox2.Height);
}
pictureBox2.Image = solidColor;
}
```c#

select, cut and replace a region of image with another [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a picture box and import image to this picture box. I want select a region of this picture box (image) like "free-form selection in MS Paint" then cut selected region and replace (fill) this region with another image or a system color.
What is your idea for this problem?!
Thanks.
I wrote a little winform application. There are a lots of things to improve here, but it can give you some advice on how to start.
I used three pictureboxes, one where I displayed the image without changes, a transparent picturebox to select the part of image that I'd like to cut and another one where I can paste the part of the image.
MAIN CODE:
Select:
//I used a rectangle to "select" the part of image
Rectangle imageRegion = new Rectangle(clickedPointOne, pbImageRegion.Size); //clickedPointOne is the point of image where I start to select and pbImageRegion.Size is the size of the part of image.
//Then I cloned the part of image that I want
Image newImage = image.Clone(imageRegion, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Cut:
//I created a new bitmap with the size of the part of image that I selected
Bitmap bmp = new Bitmap(newImage.Width, newImage.Height);
Graphics g = Graphics.FromImage(bmp);
//So here I drawed in the bitmap a rectangle with the picturebox backcolor that rappresent the "blank" part of image
g.FillRectangle(new SolidBrush(mainPictureBox.BackColor), new Rectangle(new Point(0, 0), newImage.Size));
//Now draw the "blank" part on the main image
g = Graphics.FromImage(image);
g.DrawImage(bmp, clickedPointOne);
Replace: (In my application you can paste the part of image wherever in the secondPictureBox)
//Get graphics from the picturebox image (where there could be another image)
Graphics g = Graphics.FromImage(secondPictureBox.Image);
//Draw the part of image
g.DrawImage(newImage, clickedPointTwo); //newImage is the part of image selected and cut, clickedPointTwo is the point of upper-left corner where you want to begin draw the image
THE WHOLE CODE:
private Bitmap image;
private Bitmap newImage;
private Rectangle imageRegion;
private PictureBox pbImageRegion;
private Point clickedPointOne;
private Point clickedPointTwo;
private bool allowMouseMove;
private bool clickedCutButton;
private bool firstClick;
public Form1()
{
InitializeComponent();
mainPictureBox.BackColor = Color.White;
secondPictureBox.BackColor = Color.White;
}
private void loadImageButton_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
image = new Bitmap(ofd.FileName);
mainPictureBox.Image = image;
Bitmap bmp = new Bitmap(image.Width, image.Height);
Graphics g = Graphics.FromImage(bmp);
g.FillRectangle(new SolidBrush(Color.White), new Rectangle(new Point(0, 0), secondPictureBox.Size));
secondPictureBox.Image = bmp;
}
}
private void cutImageButton_Click(object sender, EventArgs e)
{
firstClick = false;
clickedCutButton = true;
allowMouseMove = false;
pbImageRegion = new PictureBox();
pbImageRegion.BackColor = Color.Transparent;
pbImageRegion.BorderStyle = BorderStyle.FixedSingle;
pbImageRegion.Size = new Size(0, 0);
pbImageRegion.MouseMove += new MouseEventHandler(pbImageRegion_MouseMove);
}
void pbImageRegion_MouseMove(object sender, MouseEventArgs e)
{
if (allowMouseMove == true)
pbImageRegion.Size = new Size(Math.Abs(e.X - clickedPointOne.X - 2), Math.Abs(e.Y - clickedPointOne.Y - 2));
}
private void mainPictureBox_MouseClick(object sender, MouseEventArgs e)
{
if (clickedCutButton == true)
{
if (e.Button == MouseButtons.Left)
{
if (firstClick == false)
{
pbImageRegion.Location = new Point(e.X, e.Y);
mainPictureBox.Controls.Add(pbImageRegion);
clickedPointOne = new Point(e.X, e.Y);
allowMouseMove = true;
firstClick = true;
}
else
{
imageRegion = new Rectangle(clickedPointOne, pbImageRegion.Size);
newImage = image.Clone(imageRegion, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
allowMouseMove = false;
mainPictureBox.Invalidate();
}
}
}
}
private void mainPictureBox_MouseMove(object sender, MouseEventArgs e)
{
//It works only from left to right
if (allowMouseMove == true)
pbImageRegion.Size = new Size(Math.Abs(e.X - clickedPointOne.X - 2), Math.Abs(e.Y - clickedPointOne.Y - 2));
}
private void secondPictureBox_MouseClick(object sender, MouseEventArgs e)
{
if (clickedCutButton == true)
{
if (e.Button == MouseButtons.Left)
{
clickedCutButton = false;
pbImageRegion.Size = new Size(0, 0);
clickedPointTwo = new Point(e.X, e.Y);
secondPictureBox.Invalidate();
}
}
}
private void secondPictureBox_Paint(object sender, PaintEventArgs e)
{
if (newImage != null)
{
Graphics g = Graphics.FromImage(secondPictureBox.Image);
g.DrawImage(newImage, clickedPointTwo);
e.Graphics.DrawImage(secondPictureBox.Image, new Point(0, 0));
}
}
private void mainPictureBox_Paint(object sender, PaintEventArgs e)
{
if (newImage != null && allowMouseMove == false)
{
Bitmap bmp = new Bitmap(newImage.Width, newImage.Height);
Graphics g = Graphics.FromImage(bmp);
g.FillRectangle(new SolidBrush(mainPictureBox.BackColor), new Rectangle(new Point(0, 0), newImage.Size));
g = Graphics.FromImage(image);
g.DrawImage(bmp, clickedPointOne);
}
}

How do i insert a Box which i drew out from pen to a picturebox image?

i need help on inserting a box which i drew out from into a picturebox.
here is the code of the pen that i code out, i do not know how to put it in the picturebox.
there will be a webcam running on the background of the picturebox, i want my rectangle to be inside the picturebox.
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "Start")
{
Graphics myGraphics = base.CreateGraphics();
myGraphics.Clear(Color.White);
Pen myPen = new Pen(Color.DarkBlue);
Rectangle rect = new Rectangle(480, 70, 120, 120);
myGraphics.DrawRectangle(myPen, rect);
stopWebcam = false;
button1.Text = "Stop";
}
else
{
stopWebcam = true;
button1.Text = "Start";
}
}
Paining in winforms is primarily done in the OnPaint event. Your ButtonClick event handler should only setup the stage for OnPaint and possibly activate it. Example:
public class MyForm : Form
...
private Rectangle? _boxRectangle;
private void OnMyButtonClick(object sender, EventArgs e)
{
if (button1.Text == "Start")
{
_boxRectangle = new Rectangle(...);
button1.Text = "Stop";
}
else
{
_boxRectangle = null;
button1.Text = "Start";
}
Invalidate(); // repaint
}
protected override OnPaint(PaintEventArgs e)
{
if (_boxRectangle != null)
{
Graphics g = e.Graphics.
Pen pen = new Pen(Color.DarkBlue);
g.DrawRectangle(_boxRectangle);
}
}
}
You may have to draw the web cam image onto a bitmap buffer and use that as an image for the picture box.
Here is the msdn page with examples at the bottom:
http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.aspx
Here is my method for doing it.
public void GraphicsToPictureBox (ref PictureBox pb, Graphics graphics,
Int32 width, Int32 height)
{
Bitmap bitmap = new Bitmap(width,height,graphics);
pb.Image = bitmap;
}

Categories