ColrMatrix is fast way to change the image. but the color matrix apply on whole image.
is there any way to apply color matrix only a part of image rather than whole image??

Dani AI

Generated

: the usual ColorMatrix/API works on whatever you draw with it. pointed to the standard tutorial and suggested cropping — both are useful starting points. Practical choices are: draw only the region you want with ImageAttributes, draw the image twice (normal + clipped matrix pass), or copy the region to a temp bitmap, apply the matrix there, and composite it back.

A direct way (GDI+/System.Drawing) is to call DrawImage with source/destination rectangles and an ImageAttributes that has your ColorMatrix. That draws only that rectangle with the matrix:

var ia = new ImageAttributes();
ia.SetColorMatrix(colorMatrix);

Rectangle srcRect = new Rectangle(srcX, srcY, w, h);
Rectangle destRect = new Rectangle(destX, destY, w, h);

graphics.DrawImage(sourceBmp, destRect, srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height, GraphicsUnit.Pixel, ia);

If you prefer overlaying, draw the original image first, then set a clip to the target rectangle and draw the same image again with the ImageAttributes. The clip ensures only the second pass is visible in that area.

graphics.DrawImage(sourceBmp, 0, 0);
graphics.SetClip(region);
graphics.DrawImage(sourceBmp, 0, 0, sourceBmp.Width, sourceBmp.Height, GraphicsUnit.Pixel, ia);
graphics.ResetClip();

When to use which: the DrawImage-with-rect is simplest and fastest for a single rectangular area. The overlay/clip method is handy if you want the original beneath and only replace a piece. Copying the region to a temp bitmap (or using LockBits for per-pixel work) gives full control if you need complex blending. Watch alpha/pixel formats and dispose ImageAttributes and temporary bitmaps to avoid leaks; set ImageAttributes.WrapMode to TileFlipXY if you see edge artifacts when scaling.

Recommended Answers

All 3 Replies

Here is a link -

Here is a link -

Thanks for reply. sir problem is that this tutorial apply the colormatrix on whole image but i want to use the colormatrix only a part of image not the whole image.

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.