Skip to content

Commit

Permalink
- Exposed internal PrintHelper class with new CalculateEstimatedDPI(…
Browse files Browse the repository at this point in the history
…) method which can calculate DPI required for image export based on specified criterias

 - Fixed arrows display issue when printing
  • Loading branch information
panthernet committed Jan 18, 2016
1 parent fd13c12 commit 2ee9802
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 14 deletions.
7 changes: 6 additions & 1 deletion Documents/CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
RELEASE 2.3.2
RELEASE 2.3.3
DETAILED CHANGELOG:
- Exposed internal PrintHelper class with new CalculateEstimatedDPI() method which can calculate DPI required for image export based on specified criterias
- Fixed arrows display issue when printing

RELEASE 2.3.2
DETAILED CHANGELOG:
- Added GraphArea::ExportAsImageDialog() method which act as old ExportAsImage and the ExportAsImage() method now accepts filename to work without the dialog

Expand Down
1 change: 1 addition & 0 deletions GraphX.Controls/Controls/GraphArea.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2020,6 +2020,7 @@ public override void SetPrintMode(bool value, bool offsetControls = true, int ma
}
InvalidateMeasure();
UpdateLayout();
EdgesList.Values.ForEach(a=> a.UpdateEdge());
#endif
}

Expand Down
5 changes: 2 additions & 3 deletions GraphX.Controls/GraphX.WPF.Controls.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>TRACE;DEBUG;WPF</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<PlatformTarget>AnyCPU</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<DefineConstants>TRACE;WPF</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
Expand Down Expand Up @@ -206,7 +206,6 @@
<Name>GraphX.PCL.Logic</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<PropertyGroup>
Expand Down
41 changes: 38 additions & 3 deletions GraphX.Controls/PrintHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
using GraphX.PCL.Common.Enums;
using GraphX.PCL.Common.Exceptions;
using Brushes = System.Windows.Media.Brushes;
using Size = System.Windows.Size;

namespace GraphX.Controls
{
internal static class PrintHelper
public static class PrintHelper
{
/// <summary>
/// Default image resolution
Expand All @@ -21,6 +22,34 @@ internal static class PrintHelper
//Set pixelformat of image.
private static readonly PixelFormat PixelFormat = PixelFormats.Pbgra32;

/// <summary>
/// Helper method which calculates estimated image DPI based on the input criterias
/// </summary>
/// <param name="vis">GraphArea object</param>
/// <param name="imgdpi">Desired DPI</param>
/// <param name="dpiStep">DPI decrease step while estimating</param>
/// <param name="estPixelCount">Pixel quantity threshold</param>
public static double CalculateEstimatedDPI(GraphAreaBase vis, double imgdpi, double dpiStep, int estPixelCount)
{
bool result = false;
double currentDPI = imgdpi;
while (!result)
{
if (CalulateSize(vis.ContentSize.Size, currentDPI) <= estPixelCount)
result = true;
else currentDPI -= dpiStep;
if (currentDPI < 0) return 0;
}
return currentDPI;
}


private static int CalulateSize(Size desiredSize, double dpi)
{
return (int) (desiredSize.Width*(dpi/DEFAULT_DPI) + 100) *
(int) (desiredSize.Height*(dpi/DEFAULT_DPI) + 100);
}

/// <summary>
/// Method exports the GraphArea to an png image.
/// </summary>
Expand All @@ -32,7 +61,7 @@ internal static class PrintHelper
/// <param name="itype"></param>
public static void ExportToImage(GraphAreaBase surface, Uri path, ImageType itype, bool useZoomControlSurface = false, double imgdpi = DEFAULT_DPI, int imgQuality = 100)
{
if(!useZoomControlSurface)
if (!useZoomControlSurface)
surface.SetPrintMode(true, true, 100);
//Create a render bitmap and push the surface to it
UIElement vis = surface;
Expand All @@ -44,10 +73,13 @@ public static void ExportToImage(GraphAreaBase surface, Uri path, ImageType ityp
else
{
var frameworkElement = surface.Parent as FrameworkElement;
if(frameworkElement != null && frameworkElement.Parent is IZoomControl)
if (frameworkElement != null && frameworkElement.Parent is IZoomControl)
vis = ((IZoomControl) frameworkElement.Parent).PresenterVisual;
}
}



var renderBitmap =
new RenderTargetBitmap(
(int)(vis.DesiredSize.Width * (imgdpi / DEFAULT_DPI) + 100),
Expand Down Expand Up @@ -85,6 +117,8 @@ public static void ExportToImage(GraphAreaBase surface, Uri path, ImageType ityp
//Save the data to the stream
encoder.Save(outStream);
}
renderBitmap.Clear();
renderBitmap = null;
//due to mem leak in wpf :(
GC.Collect();
GC.WaitForPendingFinalizers();
Expand All @@ -95,6 +129,7 @@ public static void ExportToImage(GraphAreaBase surface, Uri path, ImageType ityp
}



public static void ShowPrintPreview(Visual surface, string description = "")
{
try
Expand Down
13 changes: 6 additions & 7 deletions GraphX.PCL.Logic/GraphX.PCL.Logic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,6 @@
<PropertyGroup>
<AssemblyOriginatorKeyFile>signature.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<!-- A reference to the entire .NET Framework is automatically included -->
<ProjectReference Include="..\GraphX.PCL.Common\GraphX.PCL.Common.csproj">
<Project>{3644d44b-dec0-4b65-bba0-c68e34821aae}</Project>
<Name>GraphX.PCL.Common</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Compile Include="..\Documents\AIV.cs">
<Link>Properties\AIV.cs</Link>
Expand Down Expand Up @@ -184,6 +177,12 @@
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\GraphX.PCL.Common\GraphX.PCL.Common.csproj">
<Project>{3644d44b-dec0-4b65-bba0-c68e34821aae}</Project>
<Name>GraphX.PCL.Common</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<PropertyGroup>
Expand Down

0 comments on commit 2ee9802

Please sign in to comment.