durazell / slimdx

Automatically exported from code.google.com/p/slimdx
MIT License
0 stars 0 forks source link

Loading Direct2D Bitmap from a file (png, jpeg, ...) #707

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,

i was locking for a method wich loads a Direct2D Bitmap from a png file. In 
didn't find anything.

This article on msdn tells me, that there would be some mechanism within 
Direct2D to perform such a task over WIC:
http://msdn.microsoft.com/en-us/library/dd756686(VS.85).aspx

My solution for now is the following workaround (Loads the bitmap from a 
System.Drawing.Bitmap object):

/// <summary>
/// Load a Direct2D bitmap from the given gdi resource.
/// </summary>
/// <param name="drawingBitmap">The gdi resource.</param>
public D2D.Bitmap LoadBitmap(Bitmap drawingBitmap)
{
    D2D.Bitmap result = null;

    //Lock the gdi resource
    BitmapData drawingBitmapData = drawingBitmap.LockBits(
        new Rectangle(0, 0, drawingBitmap.Width, drawingBitmap.Height),
        ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb);

    //Prepare loading the image from gdi resource
    DataStream dataStream = new DataStream(
        drawingBitmapData.Scan0,
        drawingBitmapData.Stride * drawingBitmapData.Height,
        true, false);
    D2D.BitmapProperties properties = new D2D.BitmapProperties();
    properties.PixelFormat = new D2D.PixelFormat(
        DXGI.Format.B8G8R8A8_UNorm,
        D2D.AlphaMode.Premultiplied);

    //Load the image from the gdi resource
    result = new D2D.Bitmap(
        m_renderTarget,
        new Size(drawingBitmap.Width, drawingBitmap.Height),
        dataStream, drawingBitmapData.Stride,
        properties);

    //Unlock the gdi resource
    drawingBitmap.UnlockBits(drawingBitmapData);

    return result;
}

It would be nice to find such a method in a future releases of SlimDX.

Greets

Original issue reported on code.google.com by roland.k...@gmail.com on 22 Jul 2010 at 10:39

GoogleCodeExporter commented 9 years ago
We don't support WIC, afaik, and won't add it until V2 at the earliest.

Original comment by josh.petrie on 27 Jul 2010 at 5:11

GoogleCodeExporter commented 9 years ago
Invalidating since V2 development is on github.

Original comment by josh.petrie on 4 Jul 2011 at 6:33