Hi I am trying to do the following two things.
1. To display a specific image from a list. Problem: I always get the first cropped part stored not the last as in the example.
2. To save the image into disk. Problem: No file is genrated on disk.
Please carefully examine the code:
//evertime the picture is cropped the mouseup event will run
public void ImageBox_MouseUp(object sender, MouseEventArgs e)if (this.drawingRectangle)this.areas.Add(this.rect);this.drawingRectangle = false;
{
Bitmap cropped = new Bitmap(rect.Width, rect.Height);Graphics g = Graphics.FromImage(cropped);g.DrawImage(bm, new Rectangle(0, 0, cropped.Width, cropped.Height), rect.X, rect.Y, cropped.Width, cropped.Height, GraphicsUnit.Pixel);this.CroppedImages.Add(bm);
pictureBox3.Image = CroppedImages.Last(); //This shows only the first cropped file, not the last one
pictureBox3.Image.Save("c:\\program files\\image.jpeg"); // This doesn't do anything no file is created
cropped.Dispose();
g.Dispose();
}
Thanks in advance
Loading
Tal KrausPosted Mar 29, 2011, 2:48 AM
FroglegPosted Mar 29, 2011, 2:16 AM
public partial class Form1 : Form
{
Rectangle cropRectangle;
Bitmap myBmp, cropBitmap;
Point pt;
bool _mouseDown = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ImageBox.Image = Image.FromFile("nodes.jpg");
}
private void ImageBox_MouseDown(object sender, MouseEventArgs e)
{
_mouseDown = true;
pt = e.Location;
}
private void ImageBox_MouseMove(object sender, MouseEventArgs e)
{
if (_mouseDown == true)
{
Graphics g = ImageBox.CreateGraphics();
ImageBox.Refresh();
cropRectangle = Rectangle.FromLTRB(pt.X, pt.Y, e.X, e.Y);
}
}
private void ImageBox_MouseUp(object sender, MouseEventArgs e)
{
_mouseDown = false;
}
private void ImageBox_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawRectangle(Pens.Red, cropRectangle);
}
private void bttnSave_Click(object sender, EventArgs e)
{
if (cropRectangle==null)
{
return;
}
myBmp = new Bitmap(ImageBox.Image,ImageBox.Width, ImageBox.Height);
cropBitmap = new Bitmap(cropRectangle.Width, cropRectangle.Height);
Graphics g1 = Graphics.FromImage(cropBitmap);
g1.DrawImage(myBmp, 0, 0);
cropBitmap.Save("thisbmp.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
I'm using a button to save, which I advise you to do also