WPF Shell拖放示例 - 不安全代码

时间:2009-07-24 03:38:06

标签: c# .net wpf shell drag-and-drop

我一直在考虑使用WPF项目中Shell Style Drag and Drop in .NET - Part 3所示的代码示例。示例项目工作正常,这是一篇很棒的文章,所以检查出来!

然而,当将代码移动到我的项目时,我在编译时收到错误“不安全的代码可能只在使用/ unsafe编译时出现”我明白我可以只更改编译选项,但是我宁愿没有不安全的代码在那里。不安全的代码如下。

如何使此代码安全?我真的没有这个领域的经验。

/// <summary>
/// Replaces any pixel with a zero alpha value with the specified transparency key.
/// </summary>
/// <param name="bmpData">The bitmap data in which to perform the operation.</param>
/// <param name="transKey">The transparency color. This color is rendered transparent
/// by the DragDropHelper.</param>
/// <remarks>
/// This function only supports 32-bit pixel formats for now.
/// </remarks>
private static void ReplaceTransparentPixelsWithTransparentKey(BitmapData bmpData, DrawingColor transKey)
{
    DrawingPixelFormat pxFormat = bmpData.PixelFormat;

    if (DrawingPixelFormat.Format32bppArgb == pxFormat
        || DrawingPixelFormat.Format32bppPArgb == pxFormat)
    {
        int transKeyArgb = transKey.ToArgb();

        // We will just iterate over the data... we don't care about pixel location,
        // just that every pixel is checked.
        unsafe
        {
            byte* pscan = (byte*)bmpData.Scan0.ToPointer();
            {
                for (int y = 0; y < bmpData.Height; ++y, pscan += bmpData.Stride)
                {
                    int* prgb = (int*)pscan;
                    for (int x = 0; x < bmpData.Width; ++x, ++prgb)
                    {
                        // If the alpha value is zero, replace this pixel's color
                        // with the transparency key.
                        if ((*prgb & 0xFF000000L) == 0L)
                            *prgb = transKeyArgb;
                    }
                }
            }
        }
    }
    else
    {
        // If it is anything else, we aren't supporting it, but we
        // won't throw, cause it isn't an error
        System.Diagnostics.Trace.TraceWarning("Not converting transparent colors to transparency key.");
        return;
    }
}

调用此代码的函数如下,也许可以通过另一种方法完全删除ReplaceTransparentPixelsWithTransparentKey函数。有什么想法吗?

/// <summary>
/// Gets a System.Drawing.Bitmap from a BitmapSource.
/// </summary>
/// <param name="source">The source image from which to create our Bitmap.</param>
/// <param name="transparencyKey">The transparency key. This is used by the DragDropHelper
/// in rendering transparent pixels.</param>
/// <returns>An instance of Bitmap which is a copy of the BitmapSource's image.</returns>
private static Bitmap GetBitmapFromBitmapSource(BitmapSource source, Color transparencyKey)
{
    // Copy at full size
    Int32Rect sourceRect = new Int32Rect(0, 0, source.PixelWidth, source.PixelHeight);

    // Convert to our destination pixel format
    DrawingPixelFormat pxFormat = ConvertPixelFormat(source.Format);

    // Create the Bitmap, full size, full rez
    Bitmap bmp = new Bitmap(sourceRect.Width, sourceRect.Height, pxFormat);
    // If the format is an indexed format, copy the color palette
    if ((pxFormat & DrawingPixelFormat.Indexed) == DrawingPixelFormat.Indexed)
        ConvertColorPalette(bmp.Palette, source.Palette);

    // Get the transparency key as a System.Drawing.Color
    DrawingColor transKey = transparencyKey.ToDrawingColor();

    // Lock our Bitmap bits, we need to write to it
    BitmapData bmpData = bmp.LockBits(
        sourceRect.ToDrawingRectangle(),
        ImageLockMode.ReadWrite,
        pxFormat);
    {
        // Copy the source bitmap data to our new Bitmap
        source.CopyPixels(sourceRect, bmpData.Scan0, bmpData.Stride * sourceRect.Height, bmpData.Stride);

        // The drag image seems to work in full 32-bit color, except when
        // alpha equals zero. Then it renders those pixels at black. So
        // we make a pass and set all those pixels to the transparency key
        // color. This is only implemented for 32-bit pixel colors for now.
        if ((pxFormat & DrawingPixelFormat.Alpha) == DrawingPixelFormat.Alpha)
            ReplaceTransparentPixelsWithTransparentKey(bmpData, transKey);
    }
    // Done, unlock the bits
    bmp.UnlockBits(bmpData);

    return bmp;
}

2 个答案:

答案 0 :(得分:1)

如果您不希望性能受到重大影响,那么您实际上没有任何其他选择。

你真的不应该害怕unsafe关键字和相关的编译器开关 - 我经常看到人们试图找到一些仍然不安全的解决方法(例如使用{{ 1}}或Win32 API),但不需要Marshal个关键字。没有任何意义 - 如果有的话,它会更有害,因为unsafe脱颖而出。

在这种情况下,例如,您当然可以将整个指针算术部件移动到C DLL或C ++ / CLI程序集中,并直接从C#或通过P / Invoke调用它。但那有什么意义呢?

答案 1 :(得分:1)

拖放的任何内容都不安全,它是图像处理代码中的指针操作。而不是自己编写代码,为什么不使用框架中现有的方法之一?

  • 使用System.Drawing.Imaging SetRemapTable(example here)通过颜色映射替换透明度。

  • 看看你是否可以使用pixel shaders处理这个问题,这会导致安全代码可能会更好地执行,因为它会利用GPU。