Explication de code Unsafe{}
Salut à tous !
J'aurais besoin de votre aide pour comprendre la partie Unsafe de ce code, car je voudrais le traduire en VB.NET :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| /// <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;
}
} |