Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

882022 - Add Sample for Adding Fallback Image for SVG #84

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34310.174
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Adding-Fallback-Image-for-SVG", "Adding-Fallback-Image-for-SVG\Adding-Fallback-Image-for-SVG.csproj", "{80D27D00-1582-46FC-966E-58B0131F030E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{80D27D00-1582-46FC-966E-58B0131F030E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{80D27D00-1582-46FC-966E-58B0131F030E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{80D27D00-1582-46FC-966E-58B0131F030E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{80D27D00-1582-46FC-966E-58B0131F030E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CAF80E3F-0CD0-4218-9A64-EBC6457337EB}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>Adding_Fallback_Image_for_SVG</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SkiaSharp" Version="2.88.8" />
<PackageReference Include="Svg.Skia" Version="1.0.0.18" />
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="25.1.41" />
</ItemGroup>

</Project>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Syncfusion.XlsIO;
using SkiaSharp;
using Svg.Skia;
namespace Adding_Fallback_Image_For_SVG
{
class Program
{
static void Main(string[] args)
{
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];

//Loads an svg image stream
FileStream svgStream = new FileStream(@"../../../Data/Image.svg", FileMode.Open);

//Convert svg stream to png stream
Stream pngStream = ConvertSvgStreamToPngStream(svgStream);

//Add svg pictures in the worksheet
worksheet.Pictures.AddPicture(1, 1, svgStream, pngStream, 400, 390);

//Saving the workbook as stream
FileStream stream = new FileStream("Svg.xlsx", FileMode.Create, FileAccess.ReadWrite);
workbook.SaveAs(stream);
stream.Dispose();
}
}
/// <summary>
/// Convert svg stream to png stream using skiasharp
/// </summary>
/// <param name="svgStream"></param>
/// <returns>"pngStream"</returns>
public static Stream ConvertSvgStreamToPngStream(Stream svgStream)
{
//Create a MemoryStream to store the converted PNG
MemoryStream pngStream = new MemoryStream();

//Load SVG image using Skiasharp
using (var svg = new SKSvg())
{
svg.Load(svgStream);

//Get the size of SVG image
var bitmap = new SKBitmap((int)svg.Picture.CullRect.Width, (int)svg.Picture.CullRect.Height);
var canvas = new SKCanvas(bitmap);

//Render SVG to the bitmap
canvas.DrawPicture(svg.Picture);

//Encode the bitmap as PNG and write to the MemoryStream
var image = SKImage.FromBitmap(bitmap);
image.Encode(SKEncodedImageFormat.Png, 100).SaveTo(pngStream);
}
return pngStream;
}
}
}