Skip to content

Commit

Permalink
Merge pull request #46 from Venkateshmuruganandam/master
Browse files Browse the repository at this point in the history
Sample added for Crop image
  • Loading branch information
MohanaselvamJothi authored Mar 15, 2024
2 parents f550005 + 2b86ac3 commit d01f29d
Show file tree
Hide file tree
Showing 17 changed files with 307 additions and 1 deletion.
25 changes: 25 additions & 0 deletions Images/Crop_and_insert_image/.NET/Crop_and_insert_image.sln
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.34414.90
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Crop_and_insert_image", "Crop_and_insert_image\Crop_and_insert_image.csproj", "{A0E70BE2-5F2E-4B4F-BD6B-39269F9C1605}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A0E70BE2-5F2E-4B4F-BD6B-39269F9C1605}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A0E70BE2-5F2E-4B4F-BD6B-39269F9C1605}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A0E70BE2-5F2E-4B4F-BD6B-39269F9C1605}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A0E70BE2-5F2E-4B4F-BD6B-39269F9C1605}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1F7E380F-E8DE-4D6C-B365-E285D96DF704}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.Presentation.Net.Core" Version="*" />
</ItemGroup>

</Project>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions Images/Crop_and_insert_image/.NET/Crop_and_insert_image/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Syncfusion.Presentation;

//Create a new instance of PowerPoint Presentation file.
using IPresentation pptxDoc = Presentation.Create();
//Add a blank slide.
ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank);
//Get a picture as stream.
using FileStream pictureStream = new FileStream(Path.GetFullPath(@"../../../Data/Image.png"), FileMode.Open);
//Add the picture to a slide by specifying its size and position.
IPicture picture = slide.Pictures.AddPicture(pictureStream, 0, 0, 250, 250);

//Apply bounding box size and position.
picture.Crop.ContainerWidth = 114.48f;
picture.Crop.ContainerHeight = 56.88f;
picture.Crop.ContainerLeft = 94.32f;
picture.Crop.ContainerTop = 128.16f;

//Apply cropping size and offsets.
picture.Crop.Width = 900.72f;
picture.Crop.Height = 74.88f;
picture.Crop.OffsetX = 329.04f;
picture.Crop.OffsetY = -9.36f;

//Save the PowerPoint Presentation.
using FileStream outputStream = new FileStream(Path.GetFullPath(@"../../../Output.pptx"), FileMode.Create);
pptxDoc.Save(outputStream);
25 changes: 25 additions & 0 deletions Images/Crop_image/.NET/Crop_image.sln
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.34414.90
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Crop_image", "Crop_image\Crop_image.csproj", "{B4FE9C68-E51F-4067-86EF-EB7578E10102}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B4FE9C68-E51F-4067-86EF-EB7578E10102}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B4FE9C68-E51F-4067-86EF-EB7578E10102}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B4FE9C68-E51F-4067-86EF-EB7578E10102}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B4FE9C68-E51F-4067-86EF-EB7578E10102}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B653DC9D-2809-4549-AC3E-99D08C965592}
EndGlobalSection
EndGlobal
14 changes: 14 additions & 0 deletions Images/Crop_image/.NET/Crop_image/Crop_image.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.Presentation.Net.Core" Version="*" />
</ItemGroup>

</Project>
Binary file not shown.
26 changes: 26 additions & 0 deletions Images/Crop_image/.NET/Crop_image/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Syncfusion.Presentation;

//Open an existing PowerPoint Presentation.
using FileStream inputStream = new FileStream(@"../../../Data/Sample.pptx", FileMode.Open, FileAccess.Read);
using IPresentation pptxDoc = Presentation.Open(inputStream);

//Retrieve the first slide from the Presentation.
ISlide slide = pptxDoc.Slides[0];
//Retrieve the first picture from the slide.
IPicture picture = slide.Pictures[0];

//Apply bounding box size and position.
picture.Crop.ContainerWidth = 114.48f;
picture.Crop.ContainerHeight = 56.88f;
picture.Crop.ContainerLeft = 94.32f;
picture.Crop.ContainerTop = 128.16f;

//Apply cropping size and offsets.
picture.Crop.Width = 900.72f;
picture.Crop.Height = 74.88f;
picture.Crop.OffsetX = 329.04f;
picture.Crop.OffsetY = -9.36f;

//Save the PowerPoint Presentation as stream.
using FileStream outputStream = new FileStream("Output.pptx", FileMode.Create);
pptxDoc.Save(outputStream);
25 changes: 25 additions & 0 deletions Images/Extract_crop_image/.NET/Extract_crop_image.sln
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.34414.90
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Extract_crop_image", "Extract_crop_image\Extract_crop_image.csproj", "{91F92AFD-D86F-4553-A366-250FD44942C1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{91F92AFD-D86F-4553-A366-250FD44942C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{91F92AFD-D86F-4553-A366-250FD44942C1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{91F92AFD-D86F-4553-A366-250FD44942C1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{91F92AFD-D86F-4553-A366-250FD44942C1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F0BC9AC2-1788-49EA-A838-305E704EDD57}
EndGlobalSection
EndGlobal
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.PresentationRenderer.Net.Core" Version="*" />
</ItemGroup>

</Project>
65 changes: 65 additions & 0 deletions Images/Extract_crop_image/.NET/Extract_crop_image/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using SkiaSharp;
using Syncfusion.Office;
using Syncfusion.Presentation;
using System.IO;


//Open an existing PowerPoint Presentation.
using FileStream fileStream = new FileStream(Path.GetFullPath(@"../../../Data/Sample.pptx"), FileMode.Open, FileAccess.Read);
using IPresentation presentation = Presentation.Open(fileStream);
//Get the first slide.
ISlide slide = presentation.Slides[0];
//Get the picture.
IPicture picture = slide.Pictures[0];
//Get cropped image stream.
using MemoryStream stream = GetCroppedPictureData(new MemoryStream(picture.ImageData), picture.Crop);
//Save the image as stream.
using FileStream fileStreamOutput = new FileStream(Path.GetFullPath(@"../../../Output.png"), FileMode.Create);
stream.CopyTo(fileStreamOutput);

// Get the cropped picture data stream.
static MemoryStream GetCroppedPictureData(MemoryStream imageStream, ICrop crop)
{
//Get the crop properties.
float imageWidth = crop.ContainerWidth;
float imageHeight = crop.ContainerHeight;
float offsetX = crop.OffsetX;
float offsetY = crop.OffsetY;
float rectWidth = crop.Width;
float rectHeight = crop.Height;
float rectX = (imageWidth - rectWidth) / 2;
float rectY = (imageHeight - rectHeight) / 2;
//Creating memory stream to return the cropped image stream.
MemoryStream outputStream = new MemoryStream();
//Create SKBitmap with respect to given image size.
using SKBitmap m_sKBitmap = new SKBitmap(PointToPixel(imageWidth), PointToPixel(imageHeight), SKImageInfo.PlatformColorType, SKAlphaType.Premul);
//Create SKSurface from the SKBitmap.
using SKSurface m_surface = SKSurface.Create(m_sKBitmap.Info);
//Draw the given image with in the image canvas.
using SKCanvas m_canvas = m_surface.Canvas;
//Set the background color of the image as transparent.
m_canvas.Clear(new SKColor(0, 0, 0, 0));
//Create SKBitmap for the given image data.
using SKBitmap inputSkBitmap = SKBitmap.Decode(SKData.Create(imageStream));
// Translate the graphics and apply the Crop.OffsetX and Crop.OffsetY values.
m_canvas.Translate(PointToPixel(offsetX), PointToPixel(offsetY));
// Draw the cropped image at the specified picture position.
SKRect srcRect = new SKRect(PointToPixel(rectX), PointToPixel(rectY), PointToPixel(rectX + rectWidth), PointToPixel(rectY + rectHeight));
// Apply the crop values.
m_canvas.DrawBitmap(inputSkBitmap, srcRect, new SKPaint() { FilterQuality = SKFilterQuality.High });

//Save the SKSurface as stream.
m_surface.Snapshot().Encode(SKEncodedImageFormat.Png, 100).SaveTo(outputStream);
outputStream.Position = 0;
return outputStream;
}

/// <summary>
/// Convert a value from point to pixel.
/// </summary>
/// <param name="value">The value in point which needs to be converted to pixel.</param>
/// <returns>The value converted to pixel.</returns>
static int PointToPixel(double value)
{
return Convert.ToInt32((value * 96) / 72);
}
25 changes: 25 additions & 0 deletions Images/Remove_crop_from_image/.NET/Remove_crop_from_image.sln
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.34414.90
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Remove_crop_from_image", "Remove_crop_from_image\Remove_crop_from_image.csproj", "{1FB8C3CA-DBB1-49D7-B59A-737AA5DD6EA8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1FB8C3CA-DBB1-49D7-B59A-737AA5DD6EA8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1FB8C3CA-DBB1-49D7-B59A-737AA5DD6EA8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1FB8C3CA-DBB1-49D7-B59A-737AA5DD6EA8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1FB8C3CA-DBB1-49D7-B59A-737AA5DD6EA8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2C75F031-13DB-42B4-9A10-CD64B7CDD47F}
EndGlobalSection
EndGlobal
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Syncfusion.Presentation;
using SkiaSharp;

//Open an existing PowerPoint Presentation.
using FileStream inputStream = new FileStream(Path.GetFullPath(@"../../../Data/Sample.pptx"), FileMode.Open, FileAccess.Read);
using IPresentation pptxDoc = Presentation.Open(inputStream);
//Retrieve the first slide from the Presentation.
ISlide slide = pptxDoc.Slides[0];
//Retrieve the first picture from the slide.
IPicture picture = slide.Pictures[0];
//Remove the crop from image.
SKBitmap bitmap = SKBitmap.Decode(new MemoryStream(picture.ImageData));
//Reset the picture with original width and height.
picture.Crop.Width = PixelToPoint(bitmap.Width);
picture.Crop.Height = PixelToPoint(bitmap.Height);
picture.Crop.ContainerWidth = PixelToPoint(bitmap.Width);
picture.Crop.ContainerHeight = PixelToPoint(bitmap.Height);
picture.Crop.OffsetX = 0;
picture.Crop.OffsetY = 0;
//Save the PowerPoint Presentation as stream.
using FileStream outputStream = new FileStream(Path.GetFullPath(@"../../../Output.pptx"), FileMode.Create);
pptxDoc.Save(outputStream);


/// <summary>
/// Convert a value from pixel to point.
/// </summary>
/// <param name="value">The value in pixel which needs to be converted to point.</param>
/// <returns>The value converted to point.</returns>
static int PixelToPoint(double value)
{
return Convert.ToInt32((value * 72) / 96);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.PresentationRenderer.Net.Core" Version="*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
//Clone the first slide of the source Presentation.
ISlide clonedSlide = sourcePresentation.Slides[0].Clone();
//Merge the cloned slide to the destination Presentation with paste option - Destination Theme.
destinationPresentation.Slides.Add(clonedSlide, PasteOptions.UseDestinationTheme, sourcePresentation);
destinationPresentation.Slides.Add(clonedSlide, PasteOptions.UseDestinationTheme);
using FileStream outputStream = new(Path.GetFullPath(@"../../../Result.pptx"), FileMode.Create, FileAccess.ReadWrite);
destinationPresentation.Save(outputStream);

0 comments on commit d01f29d

Please sign in to comment.