1

It is a code that copies 200x200 images from the x and y positions of the originalBmp and copies them to the rectbmp

There is no problem with the image and the x and y values

but rectbmp is always black

        Bitmap originalBmp = new Bitmap("C:\\Users\\park\\Downloads\\imsi\\textImage.bmp");

        
        int x = (int)P.X;
        int y = (int)P.Y;

        
        int rectWidth = 200;
        int rectHeight = 200;

        
        Bitmap rectBmp = new Bitmap(rectWidth, rectHeight);

        
        Graphics g = Graphics.FromImage(rectBmp);
        g.DrawImage(originalBmp,new Rectangle(x-100, y-100, rectWidth, rectHeight));

        
        pictureBox1.Image = originalBmp;
        rectBmp.Save("C:\\Users\\park\\Downloads\\imsi\\result.bmp", ImageFormat.Bmp);
eater Sun
  • 41
  • 4
  • Maybe try [`g.Flush()`](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.graphics.flush?view=windowsdesktop-8.0#system-drawing-graphics-flush) after drawing – Alexey S. Larionov Apr 20 '23 at 10:34
  • if you save g or throw it into a picturebox is it black? – Denis Schaf Apr 20 '23 at 10:34
  • [This answer](https://stackoverflow.com/a/734941/7034621) disposes all the objects, perhaps indeed it's not flushed properly? – orhtej2 Apr 20 '23 at 10:36
  • 2
    Wrong DrawImage() overload, the rectangle applies to rectBmp. Use [this one](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.graphics.drawimage?view=dotnet-plat-ext-7.0#system-drawing-graphics-drawimage(system-drawing-image-system-int32-system-int32-system-drawing-rectangle-system-drawing-graphicsunit)) – Hans Passant Apr 20 '23 at 10:43
  • It's not coloring it black, a blank image is always black anyway – Charlieface Apr 20 '23 at 10:48

1 Answers1

3

DrawImage should be used with correct params to properly copy the desired portion from the originalBmp to the rectBmp. You can try using DrawImage with the destination rectangle and the source rectangle to fix your issue.

using (Bitmap originalBmp = new Bitmap("C:\\Users\\park\\Downloads\\imsi\\textImage.bmp"))
{
    int x = (int)P.X;
    int y = (int)P.Y;

    int rectWidth = 200;
    int rectHeight = 200;

    using (Bitmap rectBmp = new Bitmap(rectWidth, rectHeight))
    {
        using (Graphics g = Graphics.FromImage(rectBmp))
        {
            Rectangle srcRect = new Rectangle(x - 100, y - 100, rectWidth, rectHeight);
            Rectangle destRect = new Rectangle(0, 0, rectWidth, rectHeight);
            g.DrawImage(originalBmp, destRect, srcRect, GraphicsUnit.Pixel);
        }

        pictureBox1.Image = originalBmp;
        rectBmp.Save("C:\\Users\\park\\Downloads\\imsi\\result.bmp", ImageFormat.Bmp);
    }
}

Define the source rectangle (srcRect) to be the portion of the originalBmp that we want to copy. The destination rectangle (destRect) is defined as the area of the rectBmp where the image will be drawn.

Then, we call the DrawImage with the originalBmp, destRect, srcRect, and GraphicsUnit.Pixel as your params.

Kozydot
  • 515
  • 1
  • 6