How can I determine position and size of rectangle on my image and fill rectangle with my texture using System.Drawing class or something else?

Basically I have to change the color of the particular part of an image on button click. Any idea how to do this?

Dani AI

Generated

Since will mark the area ahead of time, the cleanest, most reliable pattern is to save a same‑size mask image (PNG) where the selected region is opaque (alpha = 255 or white) and everything else is transparent (alpha = 0). At runtime load the original + mask, then either (A) do a fast per‑pixel copy using the mask to decide which pixels to replace, or (B) draw a TextureBrush/SolidBrush clipped to the mask/region. ’s Graphics.FillRectangle covers only simple rectangles; a mask supports arbitrary shapes and is editor‑friendly.

Basic workflow

  • Create a mask in any editor (same dimensions as the photo). Paint the selection as opaque and save as a 32bpp PNG.
  • In C# load both bitmaps, call a routine on button click that applies the mask to create a new image (or draw the texture into the masked area).
  • Use LockBits (not GetPixel/SetPixel) for performance, and always Dispose/using your Bitmaps.

Example: replace masked pixels with a solid color (fast, uses the mask alpha channel):

// requires: using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices;
Bitmap RecolorUsingMask(Bitmap src, Bitmap mask, Color fill)
{
    if (src.Width != mask.Width || src.Height != mask.Height) throw new ArgumentException("sizes differ");
    Bitmap result = (Bitmap)src.Clone();
    Rectangle r = new Rectangle(0,0,result.Width,result.Height);

    BitmapData resData = result.LockBits(r, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
    BitmapData maskData = mask.LockBits(r, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
    int bytes = Math.Abs(resData.Stride) * result.Height;
    byte[] resBuf = new byte[bytes], maskBuf = new byte[bytes];
    Marshal.Copy(resData.Scan0, resBuf, 0, bytes);
    Marshal.Copy(maskData.Scan0, maskBuf, 0, bytes);

    byte b = fill.B, g = fill.G, rr = fill.R, a = fill.A;
    for (int i = 0; i < bytes; i += 4)
    {
        byte mA = maskBuf[i + 3]; // mask alpha
        if (mA > 0) // selected
        {
            float t = mA / 255f; // use t to blend if desired
            resBuf[i]   = (byte)(resBuf[i]   * (1 - t) + b  * t);
            resBuf[i+1] = (byte)(resBuf[i+1] * (1 - t) + g  * t);
            resBuf[i+2] = (byte)(resBuf[i+2] * (1 - t) + rr * t);
            resBuf[i+3] = 255;
        }
    }

    Marshal.Copy(resBuf, 0, resData.Scan0, bytes);
    result.UnlockBits(resData);
    mask.UnlockBits(maskData);
    return result;
}

Notes and tips

  • For a texture fill, sample a pixel from the texture (scaled or tiled) instead of fill and blend by mask alpha.
  • To preserve highlights/lighting, use a multiply or HSL hue‑only replacement rather than overwriting RGB.
  • If building a new app, consider WPF (OpacityMask/WriteableBitmap) or SkiaSharp for richer painting/clipping APIs.
  • Common pitfalls: mask size/orientation mismatch, premultiplied alpha differences, and forgetting to Dispose bitmaps.

Recommended Answers

All 6 Replies

This is so vague. what defines a "particular part"? the idea is to just create a rectangle at "some particular location" then create a graphics object from the image then call its FillRectangle() method.

Can you be more specific: what does your image contain? ie, is it a white background with a rectangle on it which you wish to fill or is it an actual image that you need to search for a particular part of?

This is so vague. what defines a "particular part"? the idea is to just create a rectangle at "some particular location" then create a graphics object from the image then call its FillRectangle() method.

I am afraid that I need more that. I need to change color of the selected object on the picture. So basically I need to define a "particular part". I need to import picture to my windows control and select part of the object and than on button click to fill that part with the selected color. This is something that you can easily do with Flash and AS3 but I need to do something like that with C#. I am not sure that you understand me what I need to do?

Can you be more specific: what does your image contain? ie, is it a white background with a rectangle on it which you wish to fill or is it an actual image that you need to search for a particular part of?

It is an actual image. I dont have to search for a particualar part of an image, I need to mark a particular part of an image(before running program) and later on button click fill that part with selected color or texture. This is something that you can easily do with Flash and AS3.

how do you intend to mark it?
You could use some sort of pixel matching technique; search the image pixel by pixel to find any that match the area you have 'marked'

Still unclear what you expecting. Do you just want to draw a rectangle with your mouse?

"this is something you can do easily with flash" Unfortunately flash is a scripting Language on a multimedia platform. C# can be so much more than a program trapped in plug-in. But that comes with having to learn more than just how to manipulate multimedia.

if you could explain exactly what you want to do, we can help you figure out how to accomplish it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.