如何复盖原始图像与裁剪版本的图像在c#桌面应用程序

0

的问题

我是c楼#接触管理的桌面应用程序,在这里你可以选择一个虚拟形象的图像,从文件和后来的作物。 原来像是正确的显示之后作,取代了原来的一个。 然而,后来,在尝试注册的帐户出现的图像是无这样的一个错误,正在显示:

系统。基础:"价值不能为空。 (参数 '编码')"

方法上传镜像到登记页面:

        private void button_browse_Click(object sender, EventArgs e)
        {
            // select and display image in the picturebox
            OpenFileDialog opf = new OpenFileDialog();
            opf.Filter = "Select Image(*.jpg;*.png;*.gif)|*.jpg;*.png;*.gif";
            

            if(opf.ShowDialog() == DialogResult.OK)
            {
                pictureBoxProfileImage.Image = Image.FromFile(opf.FileName);
                setImage(pictureBoxProfileImage.Image);
            }
        }

该方法的剪切图片:

private void button_Select_Cropped_Area_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.Default;

            Bitmap bitmap2 = new Bitmap(pictureBoxProfileImage.Width, pictureBoxProfileImage.Height);
            pictureBoxProfileImage.DrawToBitmap(bitmap2, pictureBoxProfileImage.ClientRectangle);

            Bitmap croppedImage = new Bitmap(rectW, rectH);
            for (int x = 0; x < rectW; x++)
            {
                for (int y = 0; y < rectH; y++)
                {
                    Color pxlColor = bitmap2.GetPixel(cropX + x, cropY + y);
                    croppedImage.SetPixel(x, y, pxlColor);
                }
            }
            pictureBoxProfileImage.Image.Dispose();
            pictureBoxProfileImage.Image = (Image)croppedImage;
            pictureBoxProfileImage.SizeMode = PictureBoxSizeMode.StretchImage;
        }

这里的线路发生错误的时候救的形象:

MemoryStream picture = new MemoryStream();
pictureBoxProfileImage.Image.Save(picture, pictureBoxProfileImage.Image.RawFormat);

值得一提的是,登记册的工作正确,如果原始图像正在被传递。 我应该替换他们以某种方式(复盖原始一个与裁剪一种)?

c# crop
2021-11-15 11:20:12
1

最好的答案

1

我的猜测是,故障的发生是由于 pictureBoxProfileImage.Image.RawFormat. 你只是替换 pictureBoxProfileImage.Image但什么rawFormat做 new Bitmap(...) 有吗? 虽然我无法找到任何文档中关于它,我猜想它不具有任何有效的rawFormat。

所以,我将试图更换这样的事情 ImageFormat.Png或救rawFormat从原来的位时使用的节省。

2021-11-15 13:51:04

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................