-1

I have various reasons I cannot use the Action Paint in my PictureBox one major is I am parenting multiple pictureboxes to the main PictureBox, I am trying to figure out how using the below code to write to the image once I have placed multiple rectangles across it. I then write out the file.

        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();
            }
        }

I tried in on paint with a boolean expected to write to the main image but it did not work there are other various methods I have tried by looking at google and this search nothing came up on how not to do this by OnPaint Action using e.Graphics.

  • You need to handle the `Paint` event if you want to draw on the `PictureBox` itself, which will no affect the `Image` in the `PictureBox`. That would be like drawing on a sheet of glass over a painting. If you want draw on the `Image` in the `PictureBox` then you can do that anywhere, any time. – jmcilhinney Mar 10 '23 at 08:08
  • @jmcilhinney, I get that but how do I write to the Image? before I was writing to it fine then had to change the code to the above. It was much easier as I just write the rect I was dealing with once but now im keeping track of multiple from a list. – Silent_Coder Mar 10 '23 at 08:12
  • I was also looking at BlockFill but I did not have to do that before. I would just call pictureBox.Save. – Silent_Coder Mar 10 '23 at 08:15
  • @jmcilhinney wish i could upvote you.. But I will comment the solution so others can find it. – Silent_Coder Mar 10 '23 at 08:25
  • 1
    You should not be putting the solution in the question. That's what answers are for. You should post your own answer - I didn't post an answer because it was just general advice, not specific - and then accept it. You will likely have to wait a little while before accpting your own answer. – jmcilhinney Mar 10 '23 at 09:08
  • Do have a look at [this post,](https://stackoverflow.com/questions/27337825/picturebox-paintevent-with-other-method/27341797#27341797) which discusses the two options of drawing __into__ an image or __onto__ a control.. – TaW Mar 10 '23 at 09:12

1 Answers1

0

the final code fix was to change these lines..

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

//FIX

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

Thank you jmcilhinney really appreciate it.