winforms 如何在不使用Action Paint的情况下向pictureBox图像添加更改?

ocebsuys  于 2023-03-13  发布在  其他
关注(0)|答案(1)|浏览(110)

我有很多原因不能在我的PictureBox中使用Action Paint,其中一个主要原因是我将多个PictureBox作为主PictureBox的父级,我试图弄清楚如何使用下面的代码写入图像,一旦我在图像上放置了多个矩形。然后我写出文件。

private void Finished()
        {
            if(pictureBox.Image != null)
            {
                for(int i = 1; i < samples.Count; i++) 
                { 
                    //provide new scale against zoom feature
                    var zoom = 1f;
                    RectangleF area = Rectangle.Empty;
                    SetImageScale(pictureBox, out area, out zoom);
                    Point location = Point.Round(new PointF((samples[i].Rect.X - area.X) / zoom, (samples[i].Rect.Y - area.Y) / zoom));
                    Size size = Size.Round(new SizeF(samples[i].Rect.Width / zoom, samples[i].Rect.Height / zoom));
                    var resizedRect = new Rectangle(location, size);
                    //end provider

                    //testing if the image saves correctly
                    var hWnd = pictureBox.Handle; //this handle only writes to the window not image
                    Graphics e = Graphics.FromHwnd(hWnd);
                    using (Brush brush = new SolidBrush(Color.White))
                    {
                        var image = pictureBox.Image;
                        e.FillRectangle(brush, resizedRect);
                    }
                }
                //save the image
                pictureBox.Image.Save($"{ConfigSettings.EditedSamplesDirectory}\\{Path.GetFileName(currentImagePath)}");
                //remove image from paths
                imagePaths.Remove(currentImagePath);
                form1.Refresh();
                LoadNextImage();
            }
        }

我尝试了在油漆与布尔预期写入主图像,但它没有工作,有其他各种方法,我已经尝试了看谷歌和这个搜索没有出现如何***不***这样做的OnPaint行动使用e.Graphics。

ejk8hzay

ejk8hzay1#

最后的代码修复是更改这些行。

//testing if the image saves correctly
            var hWnd = pictureBox.Handle; //this handle only writes to the window not image
            Graphics e = Graphics.FromHwnd(hWnd);

//修复

//testing if the image saves correctly
            Graphics e = Graphics.FromImage(pictureBox.Image);

谢谢你jmcilhinney真的很感激。

相关问题