microsoft / Win2D

Win2D is an easy-to-use Windows Runtime API for immediate mode 2D graphics rendering with GPU acceleration. It is available to C#, C++ and VB developers writing apps for the Windows Universal Platform (UWP). It utilizes the power of Direct2D, and integrates seamlessly with XAML and CoreWindow.
http://microsoft.github.io/Win2D
Other
1.82k stars 287 forks source link

CanvasBitmap.LoadAsync fails in XAML designer #225

Closed weltkante closed 8 years ago

weltkante commented 8 years ago

When using CanvasImageSource in a Win10 UWP "Custom Control" to provide a custom-rendered image brush the CanvasBitmap LoadAsync call fails when the control is loaded into the VS designer.

CanvasBitmap.LoadAsync throws an exception that it cannot find the file, but when running the project the file loads just fine.

var surface = new CanvasImageSource(CanvasDevice.GetSharedDevice(), width, height, 96);
var bitmap = await CanvasBitmap.LoadAsync(CanvasDevice.GetSharedDevice(), new Uri("ms-appx:///Assets/StoreLogo.png"));

using (var dc = surface.CreateDrawingSession(Colors.Red))
using (var brush = new CanvasImageBrush(CanvasDevice.GetSharedDevice(), bitmap))
{
    brush.ExtendX = CanvasEdgeBehavior.Wrap;
    brush.ExtendY = CanvasEdgeBehavior.Wrap;

    dc.FillRectangle(width / 4, height / 4, width / 2, height / 2, brush);
}

this.Background = new ImageBrush { ImageSource = surface };

Also when I switch to a stock BitmapImage instead of loading it with Win2D it does work in the designer:

this.Background = new ImageBrush { ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/StoreLogo.png")), Stretch = Stretch.Fill };

So if BitmapImage can do it then it should be possible to get it working for Win2D and it probably is something wrong with the loading logic in CanvasBitmap.LoadAsync which makes it not work in the designer.

damyanp commented 8 years ago

Thanks for reporting this - we're planning to investigate various issues with Win2D integrating in the designer in the near future, and will add this to the list of things to check.

shawnhar commented 8 years ago

I'm not sure if this is a Win2D bug or more of a broader designer limitation. From a quick test just now, it seems that most file access APIs behave the same way - for instance File.ReadAllBytes also fails to find app resources if it is called in the designer.

It's likely that XAML is doing special things to make BitmapImage find the right data in this context, as XAML and the Visual Studio designer are tightly integrated.

weltkante commented 8 years ago

Well, I'm loading images here and not arbitrary files. If you can't do the same trick BitmapImages does to load the binary file, couldn't you just instantiate a BitmapImage to leverage its workaround and then somehow initialize the Win2D image with the data?

weltkante commented 8 years ago

I guess you are right, I can only get the BitmapImage loading if I actually put it into the visual tree.

shawnhar commented 8 years ago

Hi,

Win2D team discussed this issue but decided it isn't something we want to spend time on.

I have a feeling it may not even be possible for 3rd party code (other than XAML UI) to load resources from the app package while running in the designer. But in any case, we have other priorities and lack bandwidth to look into this.

If you are able to figure out a way to make this work and want to submit a pull request, we'd certainly be willing to accept such a contribution!

Thanks,