Problem while croping the pic

  • Thread starter Thread starter NK sharma
  • Start date Start date
N

NK sharma

Guest
Hi All,

I am croping a pic using ractangle on it.i draw ractangle and then i crop it.

But when i start drawing ractangle on pic this is no more in picture box.

I cant see pic. And i am using the cordinates of ractangle to crop the image.


Please do not give me links

Try to find problems in my code

That will be so good

I am putting the code bellow.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace TroboAki.Dialogs
{
public partial class ResizeClassPhoto : Form
{
Rectangle rc = new Rectangle();
Boolean bHaveMouse;
Point ptOriginal = new Point();
Point ptLast = new Point();
Image Pic;
private string sourcePicLocation;

public ResizeClassPhoto(string philepath)
{
InitializeComponent();
sourcePicLocation = philepath;
this.sourcePictureBox.MouseDown += new MouseEventHandler(MyMouseDown);
this.sourcePictureBox.MouseUp += new MouseEventHandler(MyMouseUp);
this.sourcePictureBox.MouseMove += new MouseEventHandler(MyMouseMove);
bHaveMouse = false;
}

public Image Image
{
set
{
this.sourcePictureBox.Image = value;
Pic = value;
}
get
{
return this.sourcePictureBox.Image;
}
}

private void ResizeClassPhoto_Load(object sender, EventArgs e)
{
this.sourcePictureBox.ImageLocation = sourcePicLocation;
}

// Called when the left mouse button is pressed.
public void MyMouseDown(Object sender, MouseEventArgs e)
{
this.sourcePictureBox.Image = Pic;
// Make a note that we "have the mouse".

bHaveMouse = true;
// Store the "starting point" for this rubber-band rectangle.
ptOriginal.X = e.X;
ptOriginal.Y = e.Y;
// Special value lets us know that no previous
// rectangle needs to be erased.
ptLast.X = -1;
ptLast.Y = -1;
}
// Convert and normalize the points and draw the reversible frame.
private void MyDrawReversibleRectangle(Point p1, Point p2)
{
Rectangle rc = new Rectangle();

// Convert the points to screen coordinates.
p1 = PointToScreen(p1);
p2 = PointToScreen(p2);
// Normalize the rectangle.
if (p1.X < p2.X)
{
rc.X = p1.X;
rc.Width = p2.X - p1.X;
}
else
{
rc.X = p2.X;
rc.Width = p1.X - p2.X;
}
if (p1.Y < p2.Y)
{
rc.Y = p1.Y;
rc.Height = p2.Y - p1.Y;
}
else
{
rc.Y = p2.Y;
rc.Height = p1.Y - p2.Y;
}
// Draw the reversible frame.
ControlPaint.DrawReversibleFrame(rc,
Color.Red, FrameStyle.Dashed);
}
// Called when the left mouse button is released.
public void MyMouseUp(Object sender, MouseEventArgs e)
{
// Set internal flag to know we no longer "have the mouse".
bHaveMouse = false;
// If we have drawn previously, draw again in that spot
// to remove the lines.
if (ptLast.X != -1)
{
Point ptCurrent = new Point(e.X, e.Y);
MyDrawReversibleRectangle(ptOriginal, ptLast);
Graphics graphics = this.sourcePictureBox.CreateGraphics();
Pen pen = new Pen(Color.Gray, 2);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
graphics.DrawRectangle(pen, rc);

}
// Set flags to know that there is no "previous" line to reverse.
ptLast.X = -1;
ptLast.Y = -1;
ptOriginal.X = -1;
ptOriginal.Y = -1;
}
// Called when the mouse is moved.
public void MyMouseMove(Object sender, MouseEventArgs e)
{
Point ptCurrent = new Point(e.X, e.Y);
// If we "have the mouse", then we draw our lines.
if (bHaveMouse)
{
// If we have drawn previously, draw again in
// that spot to remove the lines.
if (ptLast.X != -1)
{
MyDrawReversibleRectangle(ptOriginal, ptLast);
}
// Update last point.
ptLast = ptCurrent;
// Draw new lines.
MyDrawReversibleRectangle(ptOriginal, ptCurrent);
}
}
// Set up delegates for mouse events.
//protected override void OnLoad(System.EventArgs e)
//{
// FileStream fs;
// fs = new FileStream(sourcePicLocation, FileMode.Open, FileAccess.Read);
// this.sourcePictureBox.Image = System.Drawing.Image.FromStream(fs);
// fs.Close();
// //MouseDown += new MouseEventHandler(MyMouseDown);
// //MouseUp += new MouseEventHandler(MyMouseUp);
// //MouseMove += new MouseEventHandler(MyMouseMove);

//}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}

private void destinationPictureBox_Click(object sender, EventArgs e)
{

}

private void cropButton_Click(object sender, EventArgs e)
{

string filePathForCroppedImage = Path.GetDirectoryName(sourcePicLocation);
string previousImageName = Path.GetFileNameWithoutExtension(sourcePicLocation);
//Bitmap croppedBitmap;
int x, y, hight, width;
x = (int)xNumericUpDown.Value;
y = (int)yNumericUpDown.Value;
hight = (int)hightNumericUpDown.Value;
width = (int)widthNumericUpDown.Value;
Rectangle cropRect = new Rectangle(x, y, width, hight);
FileStream fs;
fs = new FileStream(sourcePicLocation, FileMode.Open, FileAccess.Read);
Image sourcePic = System.Drawing.Image.FromStream(fs);
fs.Close();
Image newImage = (System.Drawing.Image)sourcePic.Clone();
sourcePic.Dispose();
Bitmap bit = new Bitmap(newImage, 200, 200);
using (Graphics g = Graphics.FromImage(bit))
{
g.DrawImage(newImage, new Rectangle(0, 0, bit.Width, bit.Height),
cropRect,
GraphicsUnit.Pixel);
}
//croppedBitmap = new Bitmap(cropWidth, cropHeight);

//Graphics g = Graphics.FromImage(croppedBitmap);
////g.DrawImage(bit, 0, 0, rect, GraphicsUnit.Pixel);
filePathForCroppedImage += "\\" + previousImageName + "_" + "Cropped";
if (!File.Exists(filePathForCroppedImage))
Directory.CreateDirectory(filePathForCroppedImage);
MainForm.filePathForCroppedImage = filePathForCroppedImage;
//bit.Save(filePathForCroppedImage);
//Bitmap bm3 = new Bitmap(bit);
//try
//{
// bm3.Save(filePathForCroppedImage, System.Drawing.Imaging.ImageFormat.Jpeg);
//}
//catch (Exception )
//{

//}
this.destinationPictureBox.Image = bit;
}



Thank you in Advance

NK

Continue reading...
 
Back
Top