Bitmap.LockBits "Parameter invalid" error if bitmap of a certain size?

I am creating a method in which I want to take an image mask and apply it to another image. If you look at this post , you will see a frame image. The frame image in this column is maskingImage , and the background image is imageToMask . A masking image is really an image with a bright pink center. This is the process that the method goes through:

  • The masking image is PNG, and the masking image is JPG.
  • The method tracks the masked image and draws the image to mask it. This helps maintain external transparency.
  • The output form, which is then drawn under the masking image, and we make the transparent pink color transparent.

The line var bitsimageToMask = imageToMask.LockBits... is where I get my error. If the width or height of the mask for the image is less than the masking image, I get the error "The parameter is invalid." I am new when it comes to working with bitmaps.

 public Bitmap RenderMaskedImage(Bitmap maksingImage, Bitmap imageToMask, Point imageToMaskOffset, ImageFormat imageFormat) { using (var newImageToMaskGraphic = Graphics.FromImage(imageToMask)) { newImageToMaskGraphic.DrawImage(imageToMask, imageToMaskOffset); } var output = new Bitmap(maksingImage.Width, maksingImage.Height, PixelFormat.Format32bppArgb); var rect = new Rectangle(0, 0, maksingImage.Width, maksingImage.Height); var bitsMask = maksingImage.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); var bitsimageToMask = imageToMask.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); var bitsOutput = output.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); unsafe { for (int y = 0; y < maksingImage.Height; y++) { var ptrMask = (byte*)bitsMask.Scan0 + y * bitsMask.Stride; var ptrimageToMask = (byte*)bitsimageToMask.Scan0 + y * bitsimageToMask.Stride; var ptrOutput = (byte*)bitsOutput.Scan0 + y * bitsOutput.Stride; for (int x = 0; x < maksingImage.Width; x++) { ptrOutput[4 * x] = ptrimageToMask[4 * x]; // blue ptrOutput[4 * x + 1] = ptrimageToMask[4 * x + 1]; // green ptrOutput[4 * x + 2] = ptrimageToMask[4 * x + 2]; // red ptrOutput[4 * x + 3] = ptrMask[4 * x + 3]; // alpha } } } maksingImage.UnlockBits(bitsMask); imageToMask.UnlockBits(bitsimageToMask); output.UnlockBits(bitsOutput); using (var outputGraphic = Graphics.FromImage(output)) { outputGraphic.DrawImage(maksingImage.ToTransparentColor(255,0,192), 0, 0); } return output; } 
+4
source share
1 answer

The reason is because the rect that you use on imageToMask is larger than the bitmap itself.

 var rect = new Rectangle(0, 0, maksingImage.Width, maksingImage.Height); var bitsimageToMask = imageToMask.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); 

A rectangle is used to indicate the area of โ€‹โ€‹the bitmap to be locked. This rectangle can be the same size or smaller than the bitmap, but cannot be larger. In your case, because you use rect based on your maskingImage, rect becomes larger than the bitmap that you use, on which you get this error.

+5
source

All Articles