Bitmap.LockBits错误"参数无效"当位图是具有一定规模? [英] Bitmap.LockBits error "Parameter is not valid" when bitmap is a certain size?

查看:1237
本文介绍了Bitmap.LockBits错误"参数无效"当位图是具有一定规模?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个方法,我想利用图像遮罩,并将其应用到另一个图像。如果你看看这个<一href="http://stackoverflow.com/questions/13349507/overlay-image-with-png-as-mask-using-net-and-javascript">post,你会看到一个帧图像。在该职位的帧图像是 maskingImage 和背景图像是 imageToMask 。掩蔽图像确实是一个火红色的中心图像。这是该过程的方法遍历:

  1. 在掩蔽图像是PNG和图像掩码是一个JPG。
  2. 的方法跟踪屏蔽图像并绘制形象掩盖了它。这有助于保持外透明度完好无损。
  3. 在输出格式,然后将其绘制屏蔽图像下面,我们做出了粉红色的颜色是透明的。

VAR bitsimageToMask = imageToMask.LockBits ... 是我得到我的错误。如果图像的宽度或高度屏蔽比屏蔽图像小,我得到了参数无效错误。我是新手,当涉及到处理位图。

 公共位图RenderMaskedImage(位图maksingImage,位图imageToMask,点imageToMaskOffset,的imageformat的imageformat)
{
    使用(VAR newImageToMaskGraphic = Graphics.FromImage(imageToMask))
    {
    newImageToMaskGraphic.DrawImage(imageToMask,imageToMaskOffset);
    }

    无功输出=新位图(maksingImage.Width,maksingImage.Height,PixelFormat.Format32bppArgb);
    VAR RECT =新的Rectangle(0,0,maksingImage.Width,maksingImage.Height);
    VAR bitsMask = maksingImage.LockBits(矩形,ImageLockMode.ReadOnly,PixelFormat.Format32bppArgb);
    VAR bitsimageToMask = imageToMask.LockBits(矩形,ImageLockMode.ReadOnly,PixelFormat.Format32bppArgb);
    VAR bitsOutput = output.LockBits(矩形,ImageLockMode.WriteOnly,PixelFormat.Format32bppArgb);

    不安全
    {
        对于(INT Y = 0; Y&LT; 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;
            为(中间体X = 0 X  - 其中; maksingImage.Width; X ++)
            {
                ptrOutput [4 * X] = ptrimageToMask [4 * X]。 // 蓝
                ptrOutput [4 * X + 1] = ptrimageToMask [4 * X + 1]; // 绿色
                ptrOutput [4 * X + 2] = ptrimageToMask [4 * X + 2]; // 红
                ptrOutput [4 * X + 3] = ptrMask [4 * X + 3]; // α
            }
        }
    }

    maksingImage.UnlockBits(bitsMask);
    imageToMask.UnlockBits(bitsimageToMask);
    output.UnlockBits(bitsOutput);

    使用(VAR outputGraphic = Graphics.FromImage(输出))
    {
        outputGraphic.DrawImage(maksingImage.ToTransparentColor(255,0,192),0,0);
    }

    返回输出;
}
 

解决方案

原因是你在imageToMask使用矩形是更大然后是位图本身。

  VAR RECT =新的Rectangle(0,0,maksingImage.Width,maksingImage.Height);
VAR bitsimageToMask = imageToMask.LockBits(矩形,ImageLockMode.ReadOnly,PixelFormat.Format32bppArgb);
 

的矩形用于指定需要被锁定位图的区域。这个矩形可以是相同大小或比位图小,但不能做大。 你的情况,因为你用矩形根据您maskingImage的矩形变为大于你的,让你的错误使用它的位图。

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

  1. The masking image is a PNG and the image to mask is a JPG.
  2. The method traces the masking image and draws the image to mask over it. This helps keep the outer transparency intact.
  3. The output form that is then drawn underneath the masking image and we make the hot pink color transparent.

The line var bitsimageToMask = imageToMask.LockBits... is where I get my error. If the width or height of the image to mask is smaller than the masking image, I get the "Parameter is not valid" error. I am a newbie 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;
}

解决方案

The reason is the rect you are using on the imageToMask is bigger then the bitmap itself.

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

The rect is used to specify the area of the bitmap which needs to be locked. This rect can be the same size or smaller than the bitmap but can not be bigger. In your case because you use the rect based on your maskingImage the rect becomes bigger than the bitmap you are using it on which gives you that error.

这篇关于Bitmap.LockBits错误&QUOT;参数无效&QUOT;当位图是具有一定规模?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆