smourier / DirectN

Direct interop Code for .NET Framework, .NET Core and .NET 5+ : DXGI, WIC, DirectX 9 to 12, Direct2D, Direct Write, Direct Composition, Media Foundation, WASAPI, CodecAPI, GDI, Spatial Audio, DVD, Windows Media Player, UWP DXInterop, WinUI3, etc.
MIT License
311 stars 28 forks source link

I couldn't find CreatBitmapBrush #56

Closed Charltsing closed 3 months ago

Charltsing commented 3 months ago

How to use RenderTarget.CreatBitmapBrush ?

smourier commented 3 months ago

Not sure what you mean by that. What is RenderTarget?

Charltsing commented 3 months ago

There is no CreatBitmapBrush definition in ID2D1RenderTargetExtension 2

RenderTarget is ID2D1HwndRenderTarget brush

there is CreatBmpBrush in ID2D1HwndRenderTarget 33

I want to create a BitmapBrush using a ID2D1Bitmap1, and then draw this ID2D1Bitmap1 onto another ID2D1Bitmap1.

smourier commented 3 months ago

These are extension methods and they are not defined for all (~2000) interfaces in DirectN and they are not necessarily exposing all interface raw members, as they are added manually.

But their usage is always optional, and a missing extension method never prevents you from using the raw method since IComObject exposes the Object property which is a reference on the native interface. For example like this:

IComObject<ID2D1RenderTarget> rt = ... get from somewhere
rt.Object.CreateBitmapBrush( ... etc. ...).ThrowOnError( throwOnError or not):

You can also define your own extension methods if you're missing some, eg:

public static IComObject<ID2D1BitmapBrush> CreateBitmapBrush(this IComObject<ID2D1RenderTarget> renterTarget, IComObject<ID2D1Bitmap> bitmap, D2D1_BITMAP_BRUSH_PROPERTIES bitmapBrushProperties, D2D1_BRUSH_PROPERTIES? properties = null) => CreateBitmapBrush<ID2D1BitmapBrush>(renterTarget?.Object, bitmap?.Object, bitmapBrushProperties, properties);
public static IComObject<T> CreateBitmapBrush<T>(this IComObject<ID2D1RenderTarget> renterTarget, IComObject<ID2D1Bitmap> bitmap, D2D1_BITMAP_BRUSH_PROPERTIES bitmapBrushProperties, D2D1_BRUSH_PROPERTIES? properties = null) where T : ID2D1BitmapBrush => CreateBitmapBrush<T>(renterTarget?.Object, bitmap?.Object, bitmapBrushProperties, properties);
public static IComObject<T> CreateBitmapBrush<T>(this ID2D1RenderTarget renterTarget, ID2D1Bitmap bitmap, D2D1_BITMAP_BRUSH_PROPERTIES bitmapBrushProperties, D2D1_BRUSH_PROPERTIES? properties = null) where T : ID2D1BitmapBrush
{
    if (renterTarget == null)
        throw new ArgumentNullException(nameof(renterTarget));

    // handles null or not null value types
    using (var bbMem = new ComMemory(bitmapBrushProperties))
    {
        using (var pMem = new ComMemory(properties))
        {
            renterTarget.CreateBitmapBrush(bitmap, bbMem.Pointer, pMem.Pointer, out var brush).ThrowOnError();
            return new ComObject<T>((T)brush);
        }
    }
}

you can make pull requests for them if you want.

I have added CreateBitmapBrush with this commit https://github.com/smourier/DirectN/commit/fa9b24e103f5dd1c7ce25a2727b941d8505e6e4b

Charltsing commented 3 months ago

thanks for your help