Skip to content

Commit

Permalink
Close stream when markdown is drawn. refs #43
Browse files Browse the repository at this point in the history
Rlease stream when markdown is drawn.
  • Loading branch information
whistyun authored Nov 5, 2022
2 parents e81a0fc + fc7799b commit 426856f
Show file tree
Hide file tree
Showing 32 changed files with 500 additions and 47 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ jobs:
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal
env:
RUNON_GITHUB: y
4 changes: 4 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@
<BaseOutputPath>$(ArtifactsDir)bin\$(MSBuildProjectName)\</BaseOutputPath>
<PackageOutputPath>$(ArtifactsDir)nuget\$(Configuration)\</PackageOutputPath>
</PropertyGroup>

<PropertyGroup>
<PackageVersion>1.16.2</PackageVersion>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion MdXaml.Html/MdXaml.Html.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netcoreapp3.0;net45;net5.0-windows</TargetFrameworks>
<PackageId>MdXaml.Html</PackageId>
<Version>1.16.1</Version>
<Version>$(PackageVersion)</Version>
<Authors>whistyun</Authors>
<Company />
<Description>Markdown XAML processor</Description>
Expand Down
2 changes: 1 addition & 1 deletion MdXaml.Plugins/MdXaml.Plugins.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netcoreapp3.0;net45;net5.0-windows</TargetFrameworks>
<PackageId>MdXaml.Plugins</PackageId>
<Version>1.16.1</Version>
<Version>$(PackageVersion)</Version>
<Authors>whistyun</Authors>
<Company />
<Description>Markdown XAML processor</Description>
Expand Down
2 changes: 1 addition & 1 deletion MdXaml.Svg/MdXaml.Svg.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netcoreapp3.0;net45;net5.0-windows</TargetFrameworks>
<PackageId>MdXaml.Svg</PackageId>
<Version>1.16.1</Version>
<Version>$(PackageVersion)</Version>
<Authors>whistyun</Authors>
<Company />
<Description>Markdown XAML processor</Description>
Expand Down
18 changes: 16 additions & 2 deletions MdXaml.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29519.181
# Visual Studio Version 17
VisualStudioVersion = 17.3.32922.545
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MdXaml", "MdXaml\MdXaml.csproj", "{3B285B6B-C6C9-4FF5-AD2B-04808862D81E}"
EndProject
Expand Down Expand Up @@ -40,6 +40,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MdXaml.Html.Test", "tests\M
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MdXaml.Plugins", "MdXaml.Plugins\MdXaml.Plugins.csproj", "{823DB5D5-17C0-4418-895C-A28DD7D04CE9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VisualTest", "tests\VisualTest\VisualTest.csproj", "{F07D55AE-63CB-4098-9C85-8A6255A7ED54}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VisualTestApp", "tests\VisualTestApp\VisualTestApp.csproj", "{9BF2BC76-8D51-4595-B9EE-6DAF296BEC58}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -102,6 +106,14 @@ Global
{823DB5D5-17C0-4418-895C-A28DD7D04CE9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{823DB5D5-17C0-4418-895C-A28DD7D04CE9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{823DB5D5-17C0-4418-895C-A28DD7D04CE9}.Release|Any CPU.Build.0 = Release|Any CPU
{F07D55AE-63CB-4098-9C85-8A6255A7ED54}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F07D55AE-63CB-4098-9C85-8A6255A7ED54}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F07D55AE-63CB-4098-9C85-8A6255A7ED54}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F07D55AE-63CB-4098-9C85-8A6255A7ED54}.Release|Any CPU.Build.0 = Release|Any CPU
{9BF2BC76-8D51-4595-B9EE-6DAF296BEC58}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9BF2BC76-8D51-4595-B9EE-6DAF296BEC58}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9BF2BC76-8D51-4595-B9EE-6DAF296BEC58}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9BF2BC76-8D51-4595-B9EE-6DAF296BEC58}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -116,6 +128,8 @@ Global
{C539C7B4-BBF2-4AC2-A732-0C47E125CCD2} = {435867FA-EE25-4708-9BBA-8509ABC7E389}
{C45A1400-B4E9-4638-BF1F-4BB38CEC5E93} = {09BEAB2A-F47E-4D2B-AE81-8DC1BBB52638}
{CEF2B49F-90AF-4B65-AA0C-A12AB1C0A18F} = {09BEAB2A-F47E-4D2B-AE81-8DC1BBB52638}
{F07D55AE-63CB-4098-9C85-8A6255A7ED54} = {09BEAB2A-F47E-4D2B-AE81-8DC1BBB52638}
{9BF2BC76-8D51-4595-B9EE-6DAF296BEC58} = {09BEAB2A-F47E-4D2B-AE81-8DC1BBB52638}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {23DF7019-3B25-4B82-8955-25F1DDD72D84}
Expand Down
33 changes: 19 additions & 14 deletions MdXaml/ImageLoaderManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,13 @@ public async Task<Result<BitmapImage>> LoadImageAsync(IEnumerable<Uri> resourceU
{
var imgSource = new BitmapImage();
imgSource.BeginInit();
// close the stream after the BitmapImage is created
imgSource.CacheOption = BitmapCacheOption.OnLoad;
imgSource.StreamSource = stream;
imgSource.EndInit();

stream.Close();

return imgSource;
}
catch { }
Expand All @@ -156,6 +160,7 @@ public async Task<Result<BitmapImage>> LoadImageAsync(IEnumerable<Uri> resourceU
catch { }
}

stream.Close();
return null;
}

Expand Down Expand Up @@ -202,25 +207,25 @@ private static async Task<Result<Stream>> OpenStreamAsync(Uri resourceUrl)
}

return new Result<Stream>("unsupport scheme");
}

private static async Task<Stream> CheckSupportSeek(Stream stream)
{
if (stream.CanSeek)
return stream;

return await AsMemoryStream(stream);
}
static async Task<Stream> CheckSupportSeek(Stream stream)
{
if (stream.CanSeek)
return stream;

private static async Task<MemoryStream> AsMemoryStream(Stream stream)
{
var ms = new MemoryStream();
await stream.CopyToAsync(ms);
stream.Close();
return await AsMemoryStream(stream);
}

return ms;
}
static async Task<MemoryStream> AsMemoryStream(Stream stream)
{
var ms = new MemoryStream();
await stream.CopyToAsync(ms);
stream.Close();

return ms;
}
}

public class Result<T> where T : class
{
Expand Down
4 changes: 2 additions & 2 deletions MdXaml/MarkdownScrollViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ bool TryOpen(Uri path)
throw new ArgumentException($"unsupport schema {path.Scheme}");
}

var assetPathRoot = path.Scheme == "file" ? path.LocalPath : path.AbsoluteUri;
var assetPathRoot = path.Scheme == "file" ? Path.GetDirectoryName(path.LocalPath) : path.AbsoluteUri;

Engine.AssetPathRoot = assetPathRoot;

Expand Down Expand Up @@ -384,7 +384,7 @@ bool TryOpen(Uri path)
}
else
{
var assetPath = Path.Combine(AssetPathRoot, source.LocalPath);
var assetPath = Path.Combine(AssetPathRoot, source.ToString());
TryOpen(new Uri(assetPath));
}
}
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.16.1</Version>
<Version>$(PackageVersion)</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.16.1</Version>
<Version>$(PackageVersion)</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="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" /></Image.Source></Image>.</Paragraph>
<Paragraph>Images side by side <Image Tag="imageleft" Width="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" /></Image.Source></Image><Image Tag="imageright" Width="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" /></Image.Source></Image></Paragraph>
<Paragraph>A remote <Image Tag="image" Width="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" CacheOption="OnLoad" /></Image.Source></Image>.</Paragraph>
<Paragraph>Images side by side <Image Tag="imageleft" Width="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" CacheOption="OnLoad" /></Image.Source></Image><Image Tag="imageright" Width="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" CacheOption="OnLoad" /></Image.Source></Image></Paragraph>
<Paragraph>
<Image Tag="imageright" Width="48" Margin="0,0,0,0" ToolTip="cyan">
<Image.Source>
<BitmapImage BaseUri="{x:Null}" />
<BitmapImage BaseUri="{x:Null}" 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}" /></Image.Source></Image>. A local image <Image Tag="image" Width="384.1552734375" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" /></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}" 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}" 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="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" /></Image.Source></Image>.</Paragraph>
<Paragraph>Images side by side <Image Tag="imageleft" Width="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" /></Image.Source></Image><Image Tag="imageright" Width="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" /></Image.Source></Image></Paragraph>
<Paragraph>A remote <Image Tag="image" Width="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" CacheOption="OnLoad" /></Image.Source></Image>.</Paragraph>
<Paragraph>Images side by side <Image Tag="imageleft" Width="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" CacheOption="OnLoad" /></Image.Source></Image><Image Tag="imageright" Width="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" CacheOption="OnLoad" /></Image.Source></Image></Paragraph>
<Paragraph>
<Image Tag="imageright" Width="48" Margin="0,0,0,0" ToolTip="cyan">
<Image.Source>
<BitmapImage BaseUri="{x:Null}" />
<BitmapImage BaseUri="{x:Null}" 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}" /></Image.Source></Image>. A local image <Image Tag="image" Width="384.1552734375" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" /></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}" 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}" 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="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" /></Image.Source></Image>.</Paragraph>
<Paragraph>Images side by side <Image Tag="imageleft" Width="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" /></Image.Source></Image><Image Tag="imageright" Width="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" /></Image.Source></Image></Paragraph>
<Paragraph>A remote <Image Tag="image" Width="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" CacheOption="OnLoad" /></Image.Source></Image>.</Paragraph>
<Paragraph>Images side by side <Image Tag="imageleft" Width="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" CacheOption="OnLoad" /></Image.Source></Image><Image Tag="imageright" Width="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" CacheOption="OnLoad" /></Image.Source></Image></Paragraph>
<Paragraph>
<Image Tag="imageright" Width="48" Margin="0,0,0,0" ToolTip="cyan">
<Image.Source>
<BitmapImage BaseUri="{x:Null}" />
<BitmapImage BaseUri="{x:Null}" CacheOption="OnLoad" />
</Image.Source>
</Image>
</Paragraph>
<Paragraph>A resource image <Image Tag="image" Width="701.660827636719" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" /></Image.Source></Image>. A local image <Image Tag="image" Width="384.1552734375" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" /></Image.Source></Image>.</Paragraph>
<Paragraph>A resource image <Image Tag="image" Width="701.660827636719" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" 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}" 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="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" /></Image.Source></Image>.</Paragraph>
<Paragraph>Images side by side <Image Tag="imageleft" Width="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" /></Image.Source></Image><Image Tag="imageright" Width="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" /></Image.Source></Image></Paragraph>
<Paragraph>A remote <Image Tag="image" Width="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" CacheOption="OnLoad" /></Image.Source></Image>.</Paragraph>
<Paragraph>Images side by side <Image Tag="imageleft" Width="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" CacheOption="OnLoad" /></Image.Source></Image><Image Tag="imageright" Width="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" CacheOption="OnLoad" /></Image.Source></Image></Paragraph>
<Paragraph>
<Image Tag="imageright" Width="48" Margin="0,0,0,0" ToolTip="cyan">
<Image.Source>
<BitmapImage BaseUri="{x:Null}" />
<BitmapImage BaseUri="{x:Null}" 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}" /></Image.Source></Image>. A local image <Image Tag="image" Width="384.1552734375" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" /></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}" 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}" 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="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" /></Image.Source></Image>.</Paragraph>
<Paragraph>Images side by side <Image Tag="imageleft" Width="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" /></Image.Source></Image><Image Tag="imageright" Width="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" /></Image.Source></Image></Paragraph>
<Paragraph>A remote <Image Tag="image" Width="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" CacheOption="OnLoad" /></Image.Source></Image>.</Paragraph>
<Paragraph>Images side by side <Image Tag="imageleft" Width="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" CacheOption="OnLoad" /></Image.Source></Image><Image Tag="imageright" Width="48" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" CacheOption="OnLoad" /></Image.Source></Image></Paragraph>
<Paragraph>
<Image Tag="imageright" Width="48" Margin="0,0,0,0" ToolTip="cyan">
<Image.Source>
<BitmapImage BaseUri="{x:Null}" />
<BitmapImage BaseUri="{x:Null}" 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}" /></Image.Source></Image>. A local image <Image Tag="image" Width="384.1552734375" Margin="0,0,0,0"><Image.Source><BitmapImage BaseUri="{x:Null}" /></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}" 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}" CacheOption="OnLoad" /></Image.Source></Image>.</Paragraph>
</FlowDocument>
Loading

0 comments on commit 426856f

Please sign in to comment.