Posted: 6/26/2010
Let me describe my problem in detail.....
I have created a window application and implement the feature of image croping from picturebox.
Now when I select the image poration in picturebox then i got the croped image from the picturebox into another picturebox but problem is that I am unable to got the original croped image.
I means to say that size of the croped image is correct but it is croped from the actual image not from the picturebox.
So i am add the code snipet and screen shots also.
please help me out the crop the image.
namespace ImageUplaod { public partial class Form1 : Form { private long m_lImageFileLength = 0; private byte[] m_barrImg; SqlConnection con; SqlCommand sqlCommand1; Rectangle rect; Pen pen; Point startPoint; Bitmap croppedImage; bool mousePressed = false; public Form1() { InitializeComponent(); con = new SqlConnection("Data Source=RAJ\\SQLSERVER;Initial Catalog=MyNotes;User ID=sa;Password=sa"); pen = new Pen(Color.Red, 1); } protected void LoadImage() { try { this.openFileDialog1.ShowDialog(this); string strFn = this.openFileDialog1.FileName; this.pictureBox1.Image = Image.FromFile(strFn); FileInfo fiImage = new FileInfo(strFn); this.m_lImageFileLength = fiImage.Length; FileStream fs = new FileStream(strFn, FileMode.Open,FileAccess.Read, FileShare.Read); m_barrImg = new byte[Convert.ToInt32(this.m_lImageFileLength)]; int iBytesRead = fs.Read(m_barrImg, 0,Convert.ToInt32(this.m_lImageFileLength)); fs.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void btnSave_Click(object sender, EventArgs e) { try { con.Open(); sqlCommand1 = new SqlCommand("INSERT INTO tblImgData(ID,Name,Picture) values(@ID,@Name,@Picture)", con); if (sqlCommand1.Parameters.Count == 0) { sqlCommand1.Parameters.Add("@ID", System.Data.SqlDbType.Int, 4); sqlCommand1.Parameters.Add("@Name", System.Data.SqlDbType.VarChar, 50); sqlCommand1.Parameters.Add("@Picture", System.Data.SqlDbType.Image); } sqlCommand1.Parameters["@ID"].Value = editID.Text; sqlCommand1.Parameters["@Name"].Value = editName.Text; sqlCommand1.Parameters["@Picture"].Value = m_barrImg; int iresult = sqlCommand1.ExecuteNonQuery(); MessageBox.Show(Convert.ToString(iresult)); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { con.Close(); } } private void btnLoad_Click(object sender, EventArgs e) { try { SqlCommand cmdSelect = new SqlCommand("select Picture from tblImgData where ID=@ID",con); cmdSelect.Parameters.Add("@ID", SqlDbType.Int, 4); cmdSelect.Parameters["@ID"].Value = this.editID.Text; con.Open(); byte[] barrImg = (byte[])cmdSelect.ExecuteScalar(); string strfn = Convert.ToString(DateTime.Now.ToFileTime()); FileStream fs = new FileStream(strfn,FileMode.CreateNew, FileAccess.Write); fs.Write(barrImg, 0, barrImg.Length); fs.Flush(); fs.Close(); pictureBox1.Image =Image.FromFile(strfn); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { con.Close(); } } private void pictureBox1_Click(object sender, EventArgs e) { // LoadImage(); } private void DrawRect(Graphics g, Rectangle rect) { g.DrawRectangle(this.pen, rect); } public void cropImage() { try { Bitmap bmp = new Bitmap(pictureBox1.Image); this.croppedImage = bmp.Clone(this.rect,bmp.PixelFormat); this.pictureBox2.Image = (Image)this.croppedImage; this.rect = Rectangle.Empty; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void pictureBox1_Paint(object sender, PaintEventArgs e) { if (this.rect != Rectangle.Empty) { this.DrawRect(e.Graphics, this.rect); } } private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { if (pictureBox1.Image != null) { if (!this.mousePressed) { this.mousePressed = true; this.startPoint = e.Location; } } } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { if (this.mousePressed) { this.mousePressed = false; cropImage(); } } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { try { if (this.mousePressed) { int width = (e.Location.X) - (this.startPoint.X); int height = (e.Location.Y) - this.startPoint.Y; this.rect = new Rectangle(this.startPoint, new Size(width, height)); this.DrawRect(this.pictureBox1.CreateGraphics(), this.rect); this.pictureBox1.Invalidate(); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void btnBrwose_Click(object sender, EventArgs e) { LoadImage(); } private void btnZoomIn_Click(object sender, EventArgs e) { if (this.pictureBox2.Image == null) return; Size nSize = new Size(pictureBox2.Image.Width * 2, pictureBox2.Image.Height * 2); Image gdi = new Bitmap(nSize.Width, nSize.Height); Graphics ZoomInGraphics = Graphics.FromImage(gdi); ZoomInGraphics.InterpolationMode = InterpolationMode.NearestNeighbor; ZoomInGraphics.DrawImage(pictureBox2.Image, new Rectangle(new Point(0, 0), nSize), new Rectangle(new Point(0, 0),pictureBox2.Image.Size), GraphicsUnit.Pixel); ZoomInGraphics.Dispose(); pictureBox2.Image = gdi; //pictureBox2.Size = gdi.Size; } } }
Posted: 6/30/2010
Hi,
Since your issue is related to Windows Forms application then I would suggest you to post your issues at http://windowsclient.net/default.aspx to get more help from the experts there.
This site is dedicated to ASP.NET and related stuffs and I'm afraid that you will get less help when posting windows app issues here..