使用图像蒙皮的表单

介绍 你可能已经看到了TransparencyKey Windows窗体的属性。这是设置一个颜色会出现透明的输出。但不幸的是,这个属性不会工作当我们设置背景图片的形式,从图像中选择一个颜色,TransparencyKey。这是一个错误的框架。 错误:TransparencyKey属性不是有效的Windows窗体如果显示器的颜色深度设置为大于24位的值。 但不幸的是,这项决议也不会有时工作:提供查看这个链接。 背景 你可能已经读过我之前的文章关于加快Windows窗体画背景图像时使用。技术还在这里(见形式背景属性是如何覆盖)。 使用的代码 看到下面的函数,用于从一个位图对象中提取一个地区: 隐藏,收缩,复制Code

Region ^ DrawingHelper::ExtractRegion(Bitmap ^bitmap, Color transparencyKey)
{
    if (bitmap == nullptr)
        return nullptr;

    GraphicsUnit unit = GraphicsUnit::Pixel;

    System::Drawing::RectangleF boundsF = bitmap->GetBounds(unit);

    System::Drawing::Rectangle bounds =
      System::Drawing::Rectangle(safe_cast<int>(boundsF.Left), 
      safe_cast<int>(boundsF.Top), safe_cast<int>(boundsF.Width), 
      safe_cast<int>(boundsF.Height));

    //Prepare the trasperant color key

    System::UInt32 key = 
      safe_cast<System::UInt32>((transparencyKey.A << 24) | 
      (transparencyKey.R << 16) | (transparencyKey.G << 8) | 
      (transparencyKey.B << 0));

    //access to the raw bits of the image
    BitmapData ^bitmapData = bitmap->LockBits(bounds, 
           ImageLockMode::ReadOnly, PixelFormat::Format32bppArgb);

    System::UInt32* pixelPtr = (System::UInt32*)bitmapData->Scan0.ToPointer();

    //avoid property accessors in the for better perforance

    int yMax = safe_cast<int>(boundsF.Height);
    int xMax = safe_cast<int>(boundsF.Width);

    //Graphics path to keep extracted area 
    GraphicsPath ^path = gcnew GraphicsPath();

    for (int y = 0; y < yMax; y++)
    {
        //store the pointer so we can jump to next linr from it later
        System::Byte* basePos = (System::Byte*)pixelPtr;

        for (int x = 0; x < xMax; x++, pixelPtr++)
        {
            //is transparent? if yes, just continue the loop
            if (*pixelPtr == key)
                continue;

            //store where the starting position 
            int x0 = x;

            //if not transparent - scan until the next transparent byte
            while (x < xMax && *pixelPtr != key)
            {
                ++x;
                pixelPtr++;

            }

            //add the area we have found to the path
            path->AddRectangle(System::Drawing::Rectangle(x0, y, x - x0, 1));
        }

        //jump to the next line
        pixelPtr = (System::UInt32*)(basePos + bitmapData->Stride);
    }

    //now create the region from the graphic path
    Region ^region = gcnew Region(path);

    //clean up
    delete path;

    bitmap->UnlockBits(bitmapData);

    return region;
}

提取的区域设置为该地区的形式,这样我们可以在任何形式的形状。 如果你需要任何其他语言的源代码像c#或VB。网,请让我一个电子邮件。 编码快乐:) 本文转载于:http://www.diyabc.com/frontweb/news12373.html

原文地址:https://www.cnblogs.com/Dincat/p/13474072.html