Creating CroppedBitmap at runtime - not loading from resource

I am trying to write code that will load an image from a resource and then crop it. This code works when I do all or part of it in XAML. I want to move from all-XAML to all code, so I can reuse this more than one place with different Uris.

But when I try to do the same in the code, I get a DirectoryNotFoundException, because suddenly it starts looking for a folder on disk, instead of loading the image from the resource.

  • If I load BitmapImage in XAML and then create CroppedBitmap in XAML, everything works.
  • If I load BitmapImage into XAML and then write code to create CroppedBitmap, everything works.
  • If I load BitmapImage into code without creating CroppedBitmap, everything works.
  • But if I load BitmapImage into code and create CroppedBitmap in code, it tries to load from the file system instead of resources, and I get a DirectoryNotFoundException.

The following are sample code. I am sure that I am doing something stupid, but I have run all this three times (once in my real application, once in a test application and once when writing this question), and I got the same results all three times.

For all of the following code examples, I created the Images folder inside my project and added an existing image called elf.png with the default properties (Build Action = "Resource"; Output Directory = "Do Not Copy").


Case 1: Both Bitmap Image and Cropped Bitmap in XAML.

<Window x:Class="WpfApplication8.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <BitmapImage x:Key="fullImage" UriSource="Images/elf.png"/>
        <CroppedBitmap x:Key="croppedImage" Source="{StaticResource fullImage}"
                       SourceRect="0 0 240 320"/>
    </Window.Resources>
    <Image Source="{StaticResource croppedImage}"/>
</Window>

This shows the cropped part of the bitmap, as expected.


2: BitmapImage XAML; CroppedBitmap .

XAML:

<Window x:Class="WpfApplication8.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <BitmapImage x:Key="fullImage" UriSource="Images/elf.png"/>
    </Window.Resources>
    <Image Name="image"/>
</Window>

:

public Window1()
{
    InitializeComponent();
    var fullImage = (BitmapImage) FindResource("fullImage");
    var croppedImage =
        new CroppedBitmap(fullImage, new Int32Rect(0, 0, 240, 320));
    image.Source = croppedImage;
}

, .


3: BitmapImage ; CroppedBitmap.

public Window1()
{
    InitializeComponent();
    var uri = new Uri("Images/elf.png", UriKind.RelativeOrAbsolute);
    var fullImage = new BitmapImage(uri);
    image.Source = fullImage;
}

. , , , , # Uri BitmapImage .


4: BitmapImage CroppedBitmap .

    public Window1()
    {
        InitializeComponent();
        var uri = new Uri("Images/elf.png", UriKind.RelativeOrAbsolute);
        var fullImage = new BitmapImage(uri);
        var croppedImage =
            new CroppedBitmap(fullImage, new Int32Rect(0, 0, 240, 320));
        image.Source = croppedImage;
    }

, , . , , , BitmapImage , , , , BitmapImage. - , , , , . :

  • XamlParseException: " " Window1 ", " WpfApplication8, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null ". . ' Window1.xaml ' 1 13."
    • : TargetInvocationException: " ".
      • : DirectoryNotFoundException: " " C:\svn\WpfApplication8\WpfApplication8\bin\Debug\Images\elf.png '. "

, (DirectoryNotFoundException) , CroppedBitmap. , , , XAML , , .

, XAML , , , XAML:

public Window1()
{
    InitializeComponent();
    var uri = new Uri("Images/elf.png", UriKind.RelativeOrAbsolute);
    var fullImage = new BitmapImage();
    fullImage.BeginInit();
    fullImage.UriSource = uri;
    fullImage.EndInit();
    var croppedImage = new CroppedBitmap();
    croppedImage.BeginInit();
    croppedImage.Source = fullImage;
    croppedImage.SourceRect = new Int32Rect(0, 0, 240, 320);
    croppedImage.EndInit();
    image.Source = croppedImage;
}

, croppedImage.EndInit();.

, ? XAML, ?

+2
1

BitmapImage BaseUri. BaseUri, -, " ", UriSource .

BitmapImage XAML, BaseUri "pack://application:,,/WpfApplication8; component/window1.xaml". # 4, fullImage.BaseUri CroppedBitmap, .

XAML ( -BitmapImage--CroppedBitmap)

, BaseUri?

BaseUri IUriContext. IUriContext.BaseUri WPF, . , .

  • BamlRecordReader.ElementInitialize. BAML BaseUri , , IUriContext. , # 1 # 2: BAML.
  • Image.UpdateBaseUri( Source). , IUriContext, , BaseUri. , # 3 : BitmapImage GUI .

EXE, BaseUri magic://URI. ( , ), .

, BaseUri. BaseUriHelper :

fullImage.BaseUri = BaseUriHelper.GetBaseUri(this);

, fullImage BaseUri, (this). CroppedBitmap, .

+4

Source: https://habr.com/ru/post/1711531/


All Articles