Click here to Skip to main content
13,346,713 members (66,140 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 10 Mar 2009

Masking(Multiplication) of Two images using C#

, 10 Mar 2009
Rate this:
Please Sign up or sign in to vote.
This article helps to mask(multiply) second image on first image using C#.


This is my second article. In Matlab there is functionality to mask one image on other image or in words we can multiply images. Same functionality we can view in photoshop also. Using .Net we can not mask(multiply) images directly so I want to discuss code.


I am from imaging specially for analysis. In imaging it required to show image in specific shapes, and not only for display but also for analysis. For example analyze only selected part of image. Selection may be using circle, ellipse, rectangle or polygon that time we have to mask image with black and white image. This article helps developer, who work on imageing project.

Using the Code

Any body can download code use ImageProcessingLib as like plug and play to mask image and perform Dilation operation on image.

This function accepts two bitmap one for backgrond and second for foreground and function returns masked Bitmap of two images. GetPixel and SetPixel functions have several drawbacks so we decided to use Pointer. We access and modify a pixel value using Pointer. The next example utilizes the “unsafe” block in C#. Inside unsafe blocks, we have access to pointers from C#. The conclusion is that pointers in unsafe blocks are faster than GetPixel and SetPixel functions.

// This functon used to mask(multiply) two images bitmap.
public Bitmap MaskImagePtr(Bitmap SrcBitmap1, Bitmap SrcBitmap2, out string Message)
	int width;
	int height;

	Message = "";

	if (SrcBitmap1.Width < SrcBitmap2.Width)
		width = SrcBitmap1.Width;
		width = SrcBitmap2.Width;

	if (SrcBitmap1.Height < SrcBitmap2.Height)
		height = SrcBitmap1.Height;
		height = SrcBitmap2.Height;

	bitmap = new Bitmap(width, height);
	int clr1, clr2;

		BitmapData Src1Data = SrcBitmap1.LockBits(new Rectangle(0, 0, SrcBitmap1.Width, SrcBitmap1.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
		BitmapData Src2Data = SrcBitmap2.LockBits(new Rectangle(0, 0, SrcBitmap2.Width, SrcBitmap2.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
		BitmapData DestData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

			int xOffset = 3;

			for (int col = 0; col < bitmap.Height - 1; col++)
				byte* Src1Ptr = (byte*)Src1Data.Scan0 + col * Src1Data.Stride;
				byte* Src2Ptr = (byte*)Src2Data.Scan0 + col * Src2Data.Stride;
				byte* DestPtr = (byte*)DestData.Scan0 + col * DestData.Stride;

				for (int row = 0; row < bitmap.Width - 1; row++)
					clr1 = (Src1Ptr[row * xOffset] + Src1Ptr[row * xOffset + 1] + Src1Ptr[row * xOffset + 2]) / 3;
					clr2 = (Src2Ptr[row * xOffset] + Src2Ptr[row * xOffset + 1] + Src2Ptr[row * xOffset + 2]) / 3;

					clr1 *= clr2;

					if (clr1 == 0)
						DestPtr[row * xOffset] = (byte)(0);
						DestPtr[row * xOffset + 1] = (byte)(0);
						DestPtr[row * xOffset + 2] = (byte)(0);
						DestPtr[row * xOffset] = (byte)(Src2Ptr[row * xOffset]);
						DestPtr[row * xOffset + 1] = (byte)(Src2Ptr[row * xOffset + 1]);
						DestPtr[row * xOffset + 2] = (byte)(Src2Ptr[row * xOffset + 2]);


	catch (Exception ex)
		Message = ex.Message;

	return bitmap;

In this function, first we calclate minimum width and height of SrcBitmap1 and SrcBitmap2. Then create new destination bitmap for same height & width. We calculate the start and end addresses of the image structure, thinking that it is a 1D linear array. We increment the pointer from start to end addresses and multiply values both images, if multiplication is zero then store black value in destination pointer otherwise store second image pixel value in destination pointer.xOffset is used to go to next row of bits for 24 bit pixel image.

Following is list of offset for different bit images

  • 8 bit : 1
  • 16 bit : 2
  • 24 bit : 3 and
  • 32 bit : 4

Points of Interest

In this project Dilation algorithm (mathematical morphology operation) is also implemented. Also zooming operations are perform using context menu.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Kunal Pawar
Software Developer (Senior)
India India
I am Software developer, working on web and Windows technology. Willing to accept challengces and try to complete them.

You may also be interested in...


Comments and Discussions

GeneralGreat!!! Pin
tasco2-May-11 18:26
membertasco2-May-11 18:26 
Thanks for you code and guide here. Is there a way to reduce the noice generated at the edges of those white area?
GeneralSuggested enhancements Pin
robertjb2016-Mar-10 6:53
memberrobertjb2016-Mar-10 6:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 10 Mar 2009
Article Copyright 2009 by Kunal Pawar
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid