Skip to content

Commit

Permalink
Merge pull request #33 from whistyun/fixldimg
Browse files Browse the repository at this point in the history
Avoid image loading failures when an image is displayed in multiple places. #32
  • Loading branch information
whistyun authored May 9, 2022
2 parents c33706b + 32029d9 commit 5802e96
Show file tree
Hide file tree
Showing 33 changed files with 182 additions and 168 deletions.
22 changes: 18 additions & 4 deletions MdXaml/Markdown.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Cache;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
Expand Down Expand Up @@ -430,7 +432,21 @@ private Inline TreatsAsImage(Match match)
image.Width = imgSource.Width;
}

return new InlineUIContainer(image);
var container = new InlineUIContainer() { Child=image };
imgSource.DownloadFailed += (s, e) =>
{
var ext = e.ErrorException;

var label = new Label()
{
Foreground = Brushes.Red,
Content = "!" + url + "\r\n"+ext.GetType().Name + ":" + ext.Message
};

container.Child=label;
};

return container;
}

private BitmapImage MakeImage(Uri url)
Expand All @@ -443,10 +459,8 @@ private BitmapImage MakeImage(Uri url)
{
var imgSource = new BitmapImage();
imgSource.BeginInit();
imgSource.CacheOption = BitmapCacheOption.None;
imgSource.UriCachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache);
imgSource.CacheOption = BitmapCacheOption.OnLoad;
imgSource.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
imgSource.CreateOptions = BitmapCreateOptions.None;
imgSource.UriSource = url;
imgSource.EndInit();

Expand Down
2 changes: 1 addition & 1 deletion MdXaml/MdXaml.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netcoreapp3.0;net45;net5.0-windows</TargetFrameworks>
<UseWPF>true</UseWPF>
<PackageId>MdXaml</PackageId>
<Version>1.13.0</Version>
<Version>1.14.0</Version>
<Authors>Bevan Arps(original); whistyun</Authors>
<Company />
<Description>Markdown XAML processor</Description>
Expand Down
2 changes: 1 addition & 1 deletion MdXaml/MdXamlMigfree.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netcoreapp3.0;net45;net5.0-windows</TargetFrameworks>
<UseWPF>true</UseWPF>
<PackageId>MdXaml_migfree</PackageId>
<Version>1.13.0</Version>
<Version>1.14.0</Version>
<Authors>Bevan Arps(original); whistyun</Authors>
<Company />
<Description>Markdown XAML processor</Description>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="utf-16"?>
<FlowDocument Style="{x:Null}" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Paragraph>A remote <Image Tag="image" Width="1" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="http://placehold.it/350x150" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" /></Image.Source></Image>.</Paragraph>
<Paragraph>Images side by side <Image Tag="imageleft" Width="1" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="http://placehold.it/100x150/0000FF" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" /></Image.Source></Image><Image Tag="imageright" Width="1" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="http://placehold.it/100x150/00FFFF" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" /></Image.Source></Image></Paragraph>
<Paragraph>A remote <Image Tag="image" Width="1" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriSource="http://placehold.it/350x150" CreateOptions="None" CacheOption="OnLoad" /></Image.Source></Image>.</Paragraph>
<Paragraph>Images side by side <Image Tag="imageleft" Width="1" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriSource="http://placehold.it/100x150/0000FF" CreateOptions="None" CacheOption="OnLoad" /></Image.Source></Image><Image Tag="imageright" Width="1" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriSource="http://placehold.it/100x150/00FFFF" CreateOptions="None" CacheOption="OnLoad" /></Image.Source></Image></Paragraph>
<Paragraph>
<Image Tag="imageright" Width="1" Margin="0,0,0,0" ToolTip="cyan">
<Image.Source>
<BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="http://placehold.it/100x150/00FFFF" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" />
<BitmapImage BaseUri="{x:Null}" UriSource="http://placehold.it/100x150/00FFFF" CreateOptions="None" CacheOption="OnLoad" />
</Image.Source>
</Image>
</Paragraph>
<Paragraph>A resource image <Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" /></Image.Source></Image>. A local image <Image Tag="image" Width="384.1552734375" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="<assetpathroot>\ExtImg.png" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" /></Image.Source></Image>.</Paragraph>
<Paragraph>A resource image <Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="None" CacheOption="OnLoad" /></Image.Source></Image>. A local image <Image Tag="image" Width="384.1552734375" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriSource="<assetpathroot>\ExtImg.png" CreateOptions="None" CacheOption="OnLoad" /></Image.Source></Image>.</Paragraph>
</FlowDocument>
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="utf-16"?>
<FlowDocument Style="{x:Null}" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Paragraph>A remote <Image Tag="image" Width="1" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="http://placehold.it/350x150" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" /></Image.Source></Image>.</Paragraph>
<Paragraph>Images side by side <Image Tag="imageleft" Width="1" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="http://placehold.it/100x150/0000FF" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" /></Image.Source></Image><Image Tag="imageright" Width="1" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="http://placehold.it/100x150/00FFFF" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" /></Image.Source></Image></Paragraph>
<Paragraph>A remote <Image Tag="image" Width="1" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriSource="http://placehold.it/350x150" CreateOptions="None" CacheOption="OnLoad" /></Image.Source></Image>.</Paragraph>
<Paragraph>Images side by side <Image Tag="imageleft" Width="1" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriSource="http://placehold.it/100x150/0000FF" CreateOptions="None" CacheOption="OnLoad" /></Image.Source></Image><Image Tag="imageright" Width="1" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriSource="http://placehold.it/100x150/00FFFF" CreateOptions="None" CacheOption="OnLoad" /></Image.Source></Image></Paragraph>
<Paragraph>
<Image Tag="imageright" Width="1" Margin="0,0,0,0">
<Image.Source>
<BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="http://placehold.it/100x150/00FFFF" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" />
<BitmapImage BaseUri="{x:Null}" UriSource="http://placehold.it/100x150/00FFFF" CreateOptions="None" CacheOption="OnLoad" />
</Image.Source>
</Image>
</Paragraph>
<Paragraph>A resource image <Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" /></Image.Source></Image>. A local image <Image Tag="image" Width="384.1552734375" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="<assetpathroot>\ExtImg.png" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" /></Image.Source></Image>.</Paragraph>
<Paragraph>A resource image <Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="None" CacheOption="OnLoad" /></Image.Source></Image>. A local image <Image Tag="image" Width="384.1552734375" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriSource="<assetpathroot>\ExtImg.png" CreateOptions="None" CacheOption="OnLoad" /></Image.Source></Image>.</Paragraph>
</FlowDocument>
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,37 @@
<Hyperlink Command="GoToPage" CommandParameter="/foo(bar)" ToolTip="&quot;and a title&quot;&#xD;&#xA;/foo(bar)">
<Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0">
<Image.Source>
<BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" />
<BitmapImage BaseUri="{x:Null}" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="None" CacheOption="OnLoad" />
</Image.Source>
</Image>
</Hyperlink>
</Paragraph>
<Paragraph>
<Hyperlink Command="GoToPage" CommandParameter="/foo(bar)" ToolTip="&quot;and a title&quot;&#xD;&#xA;/foo(bar)">text<Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" /></Image.Source></Image></Hyperlink>
<Hyperlink Command="GoToPage" CommandParameter="/foo(bar)" ToolTip="&quot;and a title&quot;&#xD;&#xA;/foo(bar)">text<Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="None" CacheOption="OnLoad" /></Image.Source></Image></Hyperlink>
</Paragraph>
<Paragraph>
<Hyperlink Command="GoToPage" CommandParameter="/foo(bar)" ToolTip="&quot;and a title&quot;&#xD;&#xA;/foo(bar)">
<Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0">
<Image.Source>
<BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" />
<BitmapImage BaseUri="{x:Null}" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="None" CacheOption="OnLoad" />
</Image.Source>
</Image>text</Hyperlink>
</Paragraph>
<Paragraph>
<Hyperlink Command="GoToPage" CommandParameter="/foo(bar)" ToolTip="&quot;and a title&quot;&#xD;&#xA;/foo(bar)">text<Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" /></Image.Source></Image>text</Hyperlink>
<Hyperlink Command="GoToPage" CommandParameter="/foo(bar)" ToolTip="&quot;and a title&quot;&#xD;&#xA;/foo(bar)">text<Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="None" CacheOption="OnLoad" /></Image.Source></Image>text</Hyperlink>
</Paragraph>
<Paragraph>
<Hyperlink Command="GoToPage" CommandParameter="/foo(bar)" ToolTip="&quot;and a title&quot;&#xD;&#xA;/foo(bar)">text <Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" /></Image.Source></Image></Hyperlink>
<Hyperlink Command="GoToPage" CommandParameter="/foo(bar)" ToolTip="&quot;and a title&quot;&#xD;&#xA;/foo(bar)">text <Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="None" CacheOption="OnLoad" /></Image.Source></Image></Hyperlink>
</Paragraph>
<Paragraph>
<Hyperlink Command="GoToPage" CommandParameter="/foo(bar)" ToolTip="&quot;and a title&quot;&#xD;&#xA;/foo(bar)">
<Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0">
<Image.Source>
<BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" />
<BitmapImage BaseUri="{x:Null}" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="None" CacheOption="OnLoad" />
</Image.Source>
</Image> text</Hyperlink>
</Paragraph>
<Paragraph>
<Hyperlink Command="GoToPage" CommandParameter="/foo(bar)" ToolTip="&quot;and a title&quot;&#xD;&#xA;/foo(bar)">text <Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" /></Image.Source></Image> text</Hyperlink>
<Hyperlink Command="GoToPage" CommandParameter="/foo(bar)" ToolTip="&quot;and a title&quot;&#xD;&#xA;/foo(bar)">text <Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="None" CacheOption="OnLoad" /></Image.Source></Image> text</Hyperlink>
</Paragraph>
</FlowDocument>
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,37 @@
<Hyperlink Command="GoToPage" CommandParameter="/foo(bar)">
<Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0">
<Image.Source>
<BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" />
<BitmapImage BaseUri="{x:Null}" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="None" CacheOption="OnLoad" />
</Image.Source>
</Image>
</Hyperlink>
</Paragraph>
<Paragraph>
<Hyperlink Command="GoToPage" CommandParameter="/foo(bar)">text<Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" /></Image.Source></Image></Hyperlink>
<Hyperlink Command="GoToPage" CommandParameter="/foo(bar)">text<Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="None" CacheOption="OnLoad" /></Image.Source></Image></Hyperlink>
</Paragraph>
<Paragraph>
<Hyperlink Command="GoToPage" CommandParameter="/foo(bar)">
<Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0">
<Image.Source>
<BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" />
<BitmapImage BaseUri="{x:Null}" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="None" CacheOption="OnLoad" />
</Image.Source>
</Image>text</Hyperlink>
</Paragraph>
<Paragraph>
<Hyperlink Command="GoToPage" CommandParameter="/foo(bar)">text<Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" /></Image.Source></Image>text</Hyperlink>
<Hyperlink Command="GoToPage" CommandParameter="/foo(bar)">text<Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="None" CacheOption="OnLoad" /></Image.Source></Image>text</Hyperlink>
</Paragraph>
<Paragraph>
<Hyperlink Command="GoToPage" CommandParameter="/foo(bar)">text <Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" /></Image.Source></Image></Hyperlink>
<Hyperlink Command="GoToPage" CommandParameter="/foo(bar)">text <Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="None" CacheOption="OnLoad" /></Image.Source></Image></Hyperlink>
</Paragraph>
<Paragraph>
<Hyperlink Command="GoToPage" CommandParameter="/foo(bar)">
<Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0">
<Image.Source>
<BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" />
<BitmapImage BaseUri="{x:Null}" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="None" CacheOption="OnLoad" />
</Image.Source>
</Image> text</Hyperlink>
</Paragraph>
<Paragraph>
<Hyperlink Command="GoToPage" CommandParameter="/foo(bar)">text <Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriCachePolicy="BypassCache" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" /></Image.Source></Image> text</Hyperlink>
<Hyperlink Command="GoToPage" CommandParameter="/foo(bar)">text <Image Tag="image" Width="701.6608276367188" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" UriSource="pack://application:,,,/MdXaml.Test;Component/RscImg.png" CreateOptions="None" CacheOption="OnLoad" /></Image.Source></Image> text</Hyperlink>
</Paragraph>
</FlowDocument>
Loading

0 comments on commit 5802e96

Please sign in to comment.