Am developing a paint application in C# , VS2010
The start-up interface is a blank pictureBox ,
I did a mouseDown and a mouseMove event Handler for brush tool paint
and it works fine
When i try to save the new picture (After painting on the blank pictureBox)
I enter the file , and it's only a blank picture.
The problem is that the code is not saving the effects.
why?
mouseDown event handler
private void mouseDown(object sender, MouseEventArgs e)
{
if (CurrentFunction == "DrawFree")
{
if (e.Button == MouseButtons.Left)
ReleaseFlag = true;
StartPoint = e.Location;
}
}
mouseMove event handler
private void mouseMove(object sender, MouseEventArgs e)
{
if (CurrentFunction == "DrawFree")
{
if (ReleaseFlag)
{
EndPoint = e.Location;
g = pictureBox1.CreateGraphics();
g.DrawLine(p, StartPoint, EndPoint);
}
StartPoint = EndPoint;
}
}
Save code
private void savePhotoToolStripMenuItem_Click(object sender, EventArgs e)
{
using (Bitmap bmp = new Bitmap(pictureBox1.ClientSize.Width,
pictureBox1.ClientSize.Height))
{
using (Graphics g = Graphics.FromImage(bmp))
{
Image yourImage = pictureBox1.Image;
Bitmap yourBitmap = new Bitmap(yourImage);
g.DrawImage(yourBitmap,
new Rectangle(0, 0, bmp.Width, bmp.Height),
new Rectangle(0, 0, yourImage.Width, yourImage.Height),
GraphicsUnit.Pixel);
}
bmp.Save(#"d:\yourfile.png", ImageFormat.Png);
}
}
Since you Bitmapped "yourBitmap" with yourImage, try replacing bmp.Save with yourBitmap.Save
yourBitmap.Image.Save(#"d:\yourfile.png", ImageFormat.Png);
Related
I use the code provided by Erno with some modification like using DrawRect instead of FillRect to draw the selected area.
You can see the sample code in this thread :
How to select an area on a PictureBox.Image with mouse in C#
private Point RectStartPoint;
private Rectangle Rect = new Rectangle();
private Pen pen = new Pen(Color.Black, 2);
private void VisuelArchive_Load(object sender, EventArgs e)
{
pictureBox1.ImageLocation = fichierArchive;
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
}
private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
Point tempEndPoint = e.Location;
Rect.Location = new Point(Math.Min(RectStartPoint.X, tempEndPoint.X), Math.Min(RectStartPoint.Y, tempEndPoint.Y));
Rect.Size = new Size(Math.Abs(RectStartPoint.X - tempEndPoint.X), Math.Abs(RectStartPoint.Y - tempEndPoint.Y));
pictureBox1.Invalidate();
}
private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
{
// Determine the initial rectangle coordinates...
RectStartPoint = e.Location;
Invalidate();
}
private void PictureBox1_Paint(object sender, PaintEventArgs e)
{
// Draw the rectangle...
if (pictureBox1.Image != null)
{
if (Rect != null && Rect.Width > 0 && Rect.Height > 0)
{
e.Graphics.DrawRectangle(pen, Rect);
}
}
}
However I have some problem saving the selected area in a Jpg. I use this code but it seems that the Rect references are not good for that, i save a nearby area not the exact location.
Someone have an idea about how to save the exact location on the PictureBox Image ?
private void ButtonExport_Click(object sender, EventArgs e)
{
//exporter la sélection
Bitmap bmp = new Bitmap(Rect.Size.Width,Rect.Size.Height);
using (Graphics gr = Graphics.FromImage(bmp))
{
gr.DrawImage(pictureBox1.Image, new Rectangle(0, 0, Rect.Size.Width, Rect.Size.Height), Rect, GraphicsUnit.Pixel);
}
bmp.Save(Directory.GetCurrentDirectory() + "\\Archives\\" + numFiche.ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
Here is my source code. I can't seem to get the bitmap to show the lines drawn on the panel when I move the mouse with the button pressed. Frustrated and looking for someone to help me finish the code so I can complete the app for my 9-yo daughter. Thank you in advance...
namespace TV_PAINT
{
public partial class ALANA_PAINT : Form
{
Graphics g;
Pen p = new Pen(Color.Black, 7);
Point sp = new Point(0, 0);
Point ep = new Point(0, 0);
int m = 0;
Bitmap BP;
public ALANA_PAINT()
{
InitializeComponent();
tb1.Text = p.Width.ToString();
BP = new Bitmap(pnl1.ClientSize.Width, pnl1.ClientSize.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
}
private void closeButton_Click(object sender, EventArgs e)
{
pnl1.Dispose();
p.Dispose();
this.Close();
}
private void clearButton_Click(object sender, EventArgs e)
{
//pnl1.Invalidate();
p.Color = System.Drawing.Color.Black;
p.Width = 7;
tb1.Text = p.Width.ToString();
//pnl1.Invalidate();
}
private void pnl1_MouseDown(object sender, MouseEventArgs e)
{
sp = e.Location;
if (e.Button == MouseButtons.Left)
m = 1;
if (e.Button == MouseButtons.Right)
m = 1;
}
private void pnl1_MouseMove(object sender, MouseEventArgs e)
{
if (m == 1)
{
ep = e.Location;
//g = pnl1.CreateGraphics();
Graphics g = Graphics.FromImage(BP);
g.DrawLine(p, sp, ep);
}
sp = ep;
}
private void pnl1_MouseUp(object sender, MouseEventArgs e)
{
m = 0;
}
BP is just a variable in the form. As I can see, it is not displayed anywhere in your form. Why do you need a bitmap for it.
You can do something like this, just get the graphics of your form, and draw using that graphic. https://msdn.microsoft.com/en-us/library/ztxk24yx(v=vs.110).aspx
Noted: you need to do it on PaintEvent of the form, otherwise your drawing will be removed after the next repaint, so you need some variables to store all of your lines, then draw all of them in the paint event.
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.FillRectangle(myBrush, new Rectangle(0, 0, 200, 300));
myBrush.Dispose();
formGraphics.Dispose();
Updated:
If you want to save your change to a bitmap. You can use Form.DrawToBitmap to save your drawing in the form to a bitmap, then call bitmap.Save() to a file in directory.
First i'm drawing a rectangle on the pictureBox1 with the mouse
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
rect = new Rectangle(e.X, e.Y, 0, 0);
painting = true;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
rect = new Rectangle(
rect.Left,
rect.Top,
Math.Min(e.X - rect.Left, pictureBox1.ClientRectangle.Width - rect.Left),
Math.Min(e.Y - rect.Top, pictureBox1.ClientRectangle.Height - rect.Top));
}
this.pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (painting == true)
{
using (Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, rect);
}
}
}
The variable rect is global Rectangle and painting is global bool.
Then I did inside the pictureBox1 mouseup event
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
pictureBox1.Image = SaveRectanglePart(pictureBox1.Image, rect);
}
And the method SaveRectanglePart
Bitmap bmptoreturn;
public Bitmap SaveRectanglePart(Image image, RectangleF sourceRect)
{
using (var bmp = new Bitmap((int)sourceRect.Width, (int)sourceRect.Height))
{
using (var graphics = Graphics.FromImage(bmp))
{
graphics.DrawImage(image, 0.0f, 0.0f, sourceRect, GraphicsUnit.Pixel);
}
bmptoreturn = bmp;
}
return bmptoreturn;
}
What i want to do is when i finish drawing the rectangle in the mouseup event to clear the pictureBox1 and replace the image in there with the rectangle image only.
But i'm getting exception parameter not valid in the mouseup event
pictureBox1.Image = SaveBitmapPart(pictureBox1.Image, rect);
And should i dispose somewhere the variable bmptoreturn ?
In the function SaveRectanglePart the variable bmp is Dispose of before the function returns as a result of the using statement. You need to remove the using statement and the code should work.
Bitmap bmptoreturn;
public Bitmap SaveRectanglePart(Image image, RectangleF sourceRect)
{
var bmp = new Bitmap((int)sourceRect.Width, (int)sourceRect.Height)
using (var graphics = Graphics.FromImage(bmp))
{
graphics.DrawImage(image, 0.0f, 0.0f, sourceRect, GraphicsUnit.Pixel);
}
bmptoreturn = bmp;
return bmptoreturn;
}
But we have the issue of what bmptoreturn and pictureBox1.Image were referencing before they were set. The old Image/Bitmap the reference will be lost in memory until garbage collection comes along to free their memory. To be a good programmer we need to Dispose of these Image/Bitmap when we are done with them.
Image tmp = bmptoreturn;
bmptoreturn = bmp;
if(tmp != null)
tmp.Dispose();
...
Image tmp = pictureBox1.Image;
pictureBox1.Image = SaveBitmapPart(pictureBox1.Image, rect);
if(tmp != null)
tmp.Dispose();
Also, I am not sure why you are using bmptoreturn but it is not needed in the code from what I can tell. You can simply return bmp if bmptoreturn is not being used elsewhere.
I have a small app that looks pretty much like old Paint from Windows. I implemented all the Graphic using picture box Paint event. The only problem is that when I click this button a Color Dialog box should appear and let me change the color of my pen. But whenever I click the button the box never appears and my program gets stuck in the Paint event, most precisely at the line where I do the following:
pictureBox1.Image = bmp;
What I am doing wrong? Please, I would appreciate any help!
I hope the following code snippet is enough.
This is my picturebox Paint event:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
using(Graphics g = Graphics.FromImage(bmp))
{
if (lineButton && mouseIsUp)
{
g.DrawLine(myPen, mAnchorPoint, mFinalPoint);
mAnchorPoint = Point.Empty;
mFinalPoint = Point.Empty;
}
pictureBox1.Image = bmp;
}
}
And this is the button event that fires up when I wish to change my pen color:
private void ColorButton_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
if (cd.ShowDialog() == DialogResult.OK)
{
myPen.Color = cd.Color;
}
}
Don't use pictureBox1.Image = bmp; inside pictureBox1_Paint. Instead:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
using(Graphics g = Graphics.FromImage(bmp))
{
if (lineButton && mouseIsUp)
{
g.DrawLine(myPen, mAnchorPoint, mFinalPoint);
mAnchorPoint = Point.Empty;
mFinalPoint = Point.Empty;
}
//pictureBox1.Image = bmp;
}
e.Graphics.DrawImage(bmp, 0, 0);
}
Or, a better approach, set pictureBox1.Image = bmp; once, do all your drawings on bmp(not in pictureBox1_Paint) and final call pictureBox1.Invalidate();. You don't need to write code in pictureBox1_Paint().
i have picturebox1 much larger than image i'd like to load. What i want to do is aligned this image to right side, and bottom of picturebox like on screenshot:
Edit: Working
private void FromCameraPictureBox_Paint(object sender, PaintEventArgs e)
{
if (loadimage == true)
{
var image = new Bitmap(#"image.jpg");
if (image != null)
{
var g = e.Graphics;
// -- Optional -- //
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
// -- Optional -- //
g.DrawImage(image,
FromCameraPictureBox.Width - image.Width, // to right
FromCameraPictureBox.Height - image.Height, // to bottom
image.Width,
image.Height);
}
}
loadimage = false;
}
and now i want to fire paintevent from button:
void TestButtonClick(object sender, EventArgs e)
{
loadimage = true;
}
How to do this?
I'm confused why this code works nasty:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
var g = e.Graphics;
g.DrawImage(pictureBox1.Image, pictureBox1.Width - pictureBox1.Image.Width, pictureBox1.Height - pictureBox1.Image.Height);
}
EDIT:
Ok now it works:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
var image = Properties.Resources.SomeImage; //Import via Resource Manager
//Don't use pictureBox1.Image property because it will
//draw the image 2 times.
//Make sure the pictureBox1.Image property is null in Deisgn Mode
if (image != null)
{
var g = e.Graphics;
// -- Optional -- //
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
// -- Optional -- //
g.DrawImage(image,
pictureBox1.Width - image.Width, // to right
pictureBox1.Height - image.Height, // to bottom
image.Width,
image.Height);
}
}
UPDATE:
Working :) But is there any possibility to use this code without
PaintEventsArgs ? I was trying to add to my button flag and in paint
(if (flag==true) then execute Your code, but it doesn't do anything -
no drawing on picturebox1
That's because Paint event fires once. We need to make it redraw. The default redraw method for controls is Refresh();
Here you go:
bool flag = false;
Bitmap image = null;
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (flag && image != null)
{
var g = e.Graphics;
// -- Optional -- //
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
// -- Optional -- //
g.DrawImage(image,
pictureBox1.Width - image.Width,
pictureBox1.Height - image.Height,
image.Width,
image.Height);
}
}
private void button2_Click(object sender, EventArgs e)
{
image = new Bitmap("someimage.png");
flag = true;
pictureBox1.Refresh(); //Causes it repaint.
}
//If you resize the form (and anchor/dock the picturebox)
//or just resize the picturebox then you will need this:
private void pictureBox1_Resize(object sender, EventArgs e)
{
pictureBox1.Refresh();
}
You can use Bitmap + Graphics objects and copy the portion of your picture to a new bitmap (result) that will be assigned to the picturebox:
Size resultSize = new Size(100, 100);
Bitmap result = new Bitmap(resultSize.Width, resultSize.Height);
float left = yourbitmap.Width - resultSize.Width;
float top = yourbitmap.Height - resultSize.Height;
using (Graphics g = Graphics.FromImage(result))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(yourbitmap, left, top, resultSize.Width, resultSize.Height);
g.Save();
}