繁体   English   中英

如何将裁剪图像加载到picturebox c#

[英]how to load cropping image to picturebox c#

这是我的小软件。我们添加图片点击“添加您的二维码”,它将显示在图片框加载图片框,之后我将裁剪图片。 然后它显示在picturebox2上。 但我的问题是,它只显示在picturebox2 上,但是picturebox2.image 是空的......如何解决这个错误。 我想在没有空的情况下将裁剪图像添加到picturebox2 ..

在此处输入图像描述

点击裁剪按钮后在这里

在此处输入图像描述

这是我尝试过的代码

    {
        int xDown = 0;
        int yDown = 0;
        int xUp = 0;
        int yUp = 0;
        Rectangle rectCropArea = new Rectangle();
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        Task timeout;
        string fn = "";
        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {

        }

        private void selectIm_Click(object sender, EventArgs e)
        {
            OpenFileDialog opf = new OpenFileDialog();
            if (opf.ShowDialog() == DialogResult.OK)
            {
                PictureBoxLoad.Image = Image.FromFile(opf.FileName);
                fn = opf.FileName;
            }
            PictureBoxLoad.Cursor = Cursors.Cross;
        }

        private void PictureBoxLoad_MouseUp(object sender, MouseEventArgs e)
        {
           
        }

        private void PictureBoxLoad_MouseDown(object sender, MouseEventArgs e)
        {
           
        }

        private void crop_Click(object sender, EventArgs e)
        {
           
        }
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog opf = new OpenFileDialog();
            opf.Filter= "Image Files(*.jpg; *.jpeg; *.gif;*.png; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp;*.png";
            if (opf.ShowDialog() == DialogResult.OK)
            {
                PictureBoxLoad.Image = Image.FromFile(opf.FileName);
                fn = opf.FileName;
            }
            PictureBoxLoad.Cursor = Cursors.Cross;





           
        }

        private void button2_Click(object sender, EventArgs e)

        {
             string root = @"C:\FuePass";
           
            // If directory does not exist, create it. 
            if (!System.IO.Directory.Exists(root))
            {
                System.IO.Directory.CreateDirectory(root);
            }

            Bitmap bmp = new Bitmap(previewimg.Width, previewimg.Height);

            previewimg.DrawToBitmap(bmp, new Rectangle(0, 0, previewimg.Width, previewimg.Height));
            pictureBox2.DrawToBitmap(bmp, new Rectangle(pictureBox2.Location.X - previewimg.Location.X, pictureBox2.Location.Y - previewimg.Location.Y, pictureBox2.Width, pictureBox2.Height));

            bmp.Save(@"C:\Fuepass\output.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

            MessageBox.Show("Saved,Locaiton : C:\\Fuepass\\");

            //   SaveFileDialog dialog = new SaveFileDialog();
            // dialog.Filter = "JPG(*.JPG)|*.jpg";
            //  if (dialog.ShowDialog() == DialogResult.OK)
            //  {
            //      previewimg.Image.Save(dialog.FileName);
            //       pictureBox2.Image.Save(dialog.FileName);
            //  }
            // }
        }

        private void PictureBoxLoad_Click(object sender, EventArgs e)
        {

        }

        private void PictureBoxLoad_MouseDown_1(object sender, MouseEventArgs e)
        {
            PictureBoxLoad.Invalidate();

            xDown = e.X;
            yDown = e.Y;
            crop.Enabled = true;
        }

        private void PictureBoxLoad_MouseUp_1(object sender, MouseEventArgs e)
        {
            //pictureBox1.Image.Clone();
            xUp = e.X;
            yUp = e.Y;
            Rectangle rec = new Rectangle(xDown, yDown, Math.Abs(xUp - xDown), Math.Abs(yUp - yDown));
            using (Pen pen = new Pen(Color.YellowGreen, 3))
            {

                PictureBoxLoad.CreateGraphics().DrawRectangle(pen, rec);
            }
            rectCropArea = rec;
            crop.Enabled = true;
        }

        private void crop_Click_1(object sender, EventArgs e)
        {
            try
            {
                pictureBox2.Refresh();
                //Prepare a new Bitmap on which the cropped image will be drawn
                Bitmap sourceBitmap = new Bitmap(PictureBoxLoad.Image, PictureBoxLoad.Width, PictureBoxLoad.Height);
                Graphics g = pictureBox2.CreateGraphics();

                //Draw the image on the Graphics object with the new dimesions
                g.DrawImage(sourceBitmap, new Rectangle(0, 0, pictureBox2.Width, pictureBox2.Height), rectCropArea, GraphicsUnit.Pixel);
                sourceBitmap.Dispose();
                crop.Enabled = false;
                var path = Environment.CurrentDirectory.ToString();
                ms = new System.IO.MemoryStream();
                //pictureBox2.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                byte[] ar = new byte[ms.Length];
                var timeout = ms.WriteAsync(ar, 0, ar.Length);

            }
           
            catch (Exception ex)
            {

            }
            if (pictureBox2.Image == null)
            {
                MessageBox.Show("no image");
            }
        }
    }
}

plz tell me the error

您应该能够将图像设置为位图。 但不要在保存之前处理位图。 希望这可以帮助。

        private void crop_Click_1(object sender, EventArgs e)
        {
            try
            {
                pictureBox2.Refresh();
                //Prepare a new Bitmap on which the cropped image will be drawn
                Bitmap sourceBitmap = new Bitmap(PictureBoxLoad.Image, PictureBoxLoad.Width, PictureBoxLoad.Height);
                Graphics g = pictureBox2.CreateGraphics();

                //Draw the image on the Graphics object with the new dimesions
                g.DrawImage(sourceBitmap, new Rectangle(0, 0, pictureBox2.Width, pictureBox2.Height), rectCropArea, GraphicsUnit.Pixel);
                
                crop.Enabled = false;
                var path = Environment.CurrentDirectory.ToString();
                ms = new System.IO.MemoryStream();
                pictureBox2.Image = sourceBitmap;
                pictureBox2.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                byte[] ar = new byte[ms.Length];
                var timeout = ms.WriteAsync(ar, 0, ar.Length);
                sourceBitmap.Dispose();

            }

            catch (Exception ex)
            {

            }
            if (pictureBox2.Image == null)
            {
                MessageBox.Show("no image");
            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM