Ejemplo n.º 1
0
    /// <inheritdoc/>
    public override byte[] GetBytes(Bitmap bitmap, BitmapWriterFlags wFlags)
    {
        // default - this will cause GDI to convert the pixel format to bgra32 if we don't know the format directly
        var gdiFmt  = PixelFormat.Format32bppArgb;
        var coreFmt = BitmapCorePixelFormat.Bgra32;

        var pxarr = Formats.Where(f => f.gdiFmt == bitmap.PixelFormat).ToArray();

        if (pxarr.Length > 0)
        {
            var px = pxarr.First();
            gdiFmt  = px.gdiFmt;
            coreFmt = px.coreFmt;
        }

        var colorTable = bitmap.Palette.Entries.Select(e => new RGBQUAD {
            rgbBlue = e.B, rgbGreen = e.G, rgbRed = e.R
        }).ToArray();

        var dlock = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, gdiFmt);
        var buf   = (byte *)dlock.Scan0;

        BITMAP_WRITE_REQUEST req = new BITMAP_WRITE_REQUEST
        {
            dpiX          = 0,
            dpiY          = 0,
            imgWidth      = bitmap.Width,
            imgHeight     = bitmap.Height,
            imgStride     = (uint)dlock.Stride,
            imgTopDown    = true,
            imgColorTable = colorTable,
        };

        var bytes = BitmapCore.WriteToBMP(ref req, buf, coreFmt, (uint)wFlags);

        bitmap.UnlockBits(dlock);
        return(bytes);
    }
Ejemplo n.º 2
0
    /// <summary>
    /// Write a bitmap to a byte array.
    /// </summary>
    public static byte[] ToBytes(TBitmap bitmap, BitmapWriterFlags wFlags)
    {
        var impl = new TSelf();

        return(impl.GetBytes(bitmap, wFlags));
    }
Ejemplo n.º 3
0
 /// <inheritdoc/>
 public abstract byte[] GetBytes(TBitmap bitmap, BitmapWriterFlags wFlags);
Ejemplo n.º 4
0
 /// <inheritdoc/>
 public override byte[] GetBytes(BitmapSource bitmap, BitmapWriterFlags wFlags)
 {
     return(BitmapWpfInternal.GetBytes(bitmap, (uint)wFlags));
 }