Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

c# crop from picturebox image and give original size

$
0
0

Hi,

I need your help to solve my problem,

I need to crop image from picture box , and i want to save cropped are as per original image.

eg. if image has 2448x3264 dimension and it is scaled in  picturebox size like 340x480 and picturebox.sizemode value is zoom.

so when i am cropping the image from picturebox it is giving me small picture but i want to same cropped image from original picture so result will be big.

Below is my code

        int cropX;
        int cropY;
        int cropWidth;
        int cropHeight;
        Pen cropPen = new Pen(Color.Yellow, 2);
        Bitmap  cropBitmap ;
        Bitmap  bm_dest ;
        Bitmap bm_source ;
        int i = 1;
        FolderBrowserDialog s_fold=new FolderBrowserDialog();
        FolderBrowserDialog d_fold = new FolderBrowserDialog();

        string[] files;
        int pheight;
        int pwidth;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox1.Size = new Size(pictureBox1.Width, Height-(Height*5/100));
            pheight = pictureBox1.Height;
            pwidth = pictureBox1.Width;


            
            //            pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dlg = new OpenFileDialog())
            {
                dlg.Title = "Open Image";
                dlg.Filter = "Jpge files (*.jpg)|*.jpg";

                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    //            PictureBox PictureBox1 = new PictureBox();

                    // Create a new Bitmap object from the picture file on disk,
                    // and assign that to the PictureBox.Image property

                    pictureBox1.Width = pwidth;
                    pictureBox1.Height = pheight;

                    pictureBox1.Image = new Bitmap(dlg.FileName);
                    Type pboxType = pictureBox1.GetType();
                         PropertyInfo irProperty = pboxType.GetProperty("ImageRectangle", BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Instance);
                    Rectangle rectangle = (Rectangle)irProperty.GetValue(pictureBox1, null);

                    pictureBox1.Height = rectangle.Height;
                    pictureBox1.Width = rectangle.Width;
    

                }
            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                cropX = e.X;
                cropY = e.Y;
                cropPen.DashStyle = DashStyle.Solid;
                Cursor = Cursors.Cross;
            }
            pictureBox1.Refresh();
        }



        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (pictureBox1.Image == null)
                    return;

                pictureBox1.Refresh();
                cropWidth = e.X - cropX;
                cropHeight = e.Y - cropY;
                Graphics g = pictureBox1.CreateGraphics();
                g.DrawRectangle(cropPen, cropX, cropY, cropWidth, cropHeight);

           


            }

        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
           try
            {
                Cursor = Cursors.Default;
               try
                {

                    if (cropWidth < 1)
                        return;

                    Rectangle rect = new Rectangle(cropX, cropY, cropWidth, cropHeight);
                    Bitmap bit = new Bitmap(pictureBox1.Image, pictureBox1.Width, pictureBox1.Height);

                    cropBitmap = new Bitmap(cropWidth, cropHeight);
                    Graphics g = Graphics.FromImage(cropBitmap);
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                    g.DrawImage(bit, 0, 0, rect, GraphicsUnit.Pixel);

                  
//                    try
  //                  {
                      //  cropBitmap.Save("d:\\chetan_.jpg");
    //                }
      //              catch (Exception ex) { MessageBox.Show(ex.Message); } 
                        cropBitmap.Save("d:\\abc.jpg");

                }
                catch { }
            }
            catch { }
        }

      


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>