0

I'm trying to cut out a separate part from my screen and save this as an png. But I dont know why my Image im getting is empty.

This is my function I use:

private void createImage(Point pointTL, Point pointRD)
        {
            Size size = new Size();
            size.Width = pointRD.X - pointTL.X;
            size.Height = pointRD.Y - pointTL.Y;

            //Create a new bitmap.
            var targetBitmap = new Bitmap(size.Width,
                                           size.Height,
                                           PixelFormat.Format32bppArgb);

            Rectangle targetRectangle = new Rectangle(pointTL.X, pointTL.Y, size.Width, size.Height);
            Rectangle sourceRectangle = new Rectangle(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);

            using(Bitmap sourceBitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                              Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb))

            using (Graphics g = Graphics.FromImage(targetBitmap))
                g.DrawImage(sourceBitmap, targetRectangle, sourceRectangle, GraphicsUnit.Pixel);

            if (pictureBoxScreenshot.Image != null)
            {
                pictureBoxScreenshot.Dispose();
                pictureBoxScreenshot.Image = targetBitmap;
            }
            targetBitmap.Save("Screenshot.png", System.Drawing.Imaging.ImageFormat.Png);
        }

So my Screenshot im getting looks like this. There is just nothing.

dev.doc
  • 569
  • 3
  • 12
  • 18
  • See duplicate for working code. If you have trouble getting that to work, post a new question in which you've provided a [mcve] and a clear, detailed explanation of what the code does, what you want it to do instead, and what _specifically_ you are having trouble figuring out. – Peter Duniho Jul 23 '20 at 18:19
  • sourceBitmap is empty. – TaW Jul 24 '20 at 03:35

0 Answers0