Click here to Skip to main content
15,868,016 members
Articles / Multimedia / GDI+

Masking(Multiplication) of Two images using C#

Rate me:
Please Sign up or sign in to vote.
2.90/5 (6 votes)
10 Mar 2009CPOL2 min read 54.8K   2.8K   18   2
This article helps to mask(multiply) second image on first image using C#.

Introduction

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.

Purpose

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;
	else
		width = SrcBitmap2.Width;

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

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

	try
	{
		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);

		unsafe
		{
			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);
					}
					else
					{
						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]);
					}
				}
			}
		}

		bitmap.UnlockBits(DestData);
		SrcBitmap1.UnlockBits(Src1Data);
		SrcBitmap2.UnlockBits(Src2Data);

		SrcBitmap1.Dispose();
		SrcBitmap2.Dispose();
	}
	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.

License

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


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

Comments and Discussions

 
GeneralGreat!!! Pin
tasco2-May-11 17:26
tasco2-May-11 17:26 
GeneralSuggested enhancements Pin
robertjb2016-Mar-10 5:53
professionalrobertjb2016-Mar-10 5: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.