0

I wrote simple extension to draw things on Bitmap:

public static void Fill(this Bitmap src, Contour contour, Color color, int xOffset = 0, int yOffset = 0)
{
    foreach (var point in contour.Points)
    {
        src.SetPixel(point, color, xOffset, yOffset);
    }
}

Then I realized, that this method is changing the src object, which is problematic for me. I changed this method to:

public static Bitmap Fill(this Bitmap src, Contour contour, Color color, int xOffset = 0, int yOffset = 0)
{
    var dst = new Bitmap(src);

    foreach (var point in contour.Points)
    {
        dst.SetPixel(point, color, xOffset, yOffset);
    }

    return dst;
}

Now, src object is immutable. Unfortunately, now my method is not working properly. No changes are aplied to dst object. Why?

KeyvanSFX
  • 113
  • 1
  • 11
dafie
  • 951
  • 7
  • 25
  • 3
    Please [edit] your question to include the source code you have as a [mcve], which can be compiled and tested by others. We cannot see how you use your method or how you use the bitmap you created. – Progman Jun 28 '20 at 20:49
  • You say that the first method was *'changing the `src` object, which is problematic'*. Did it produce the expected results on the bitmap though? – Andrew Williamson Jun 28 '20 at 21:50
  • Also, just as an aside - since Bitmap is a mutable object, I would expect the `Fill` method to mutate the bitmap that it operates on. I think it would be more consistent if you keep the first method, and make a separate `Clone` extension method – Andrew Williamson Jun 28 '20 at 21:52
  • Does this answer your question? [How to create a Bitmap deep copy](https://stackoverflow.com/questions/5882815/how-to-create-a-bitmap-deep-copy) – Julian Mar 03 '23 at 18:17

0 Answers0