DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Python Techniques for Text Extraction From Images
  • OneStream Fast Data Extracts APIs
  • Reversing an Array: An Exploration of Array Manipulation
  • Setup ActiveMQ Artemis on Windows

Trending

  • AI-Assisted Coding for iOS Development: How Tools like CursorAI Are Redefining the Developer Workflow
  • Scalability 101: How to Build, Measure, and Improve It
  • Selenium Pagination Tutorial: How to Handle Page Navigation
  • How Clojure Shapes Teams and Products

How to Access & Extract Images from Presentation Shapes inside .NET Apps

By 
David Zondray user avatar
David Zondray
·
Jul. 01, 15 · Code Snippet
Likes (0)
Comment
Save
Tweet
Share
805 Views

Join the DZone community and get the full member experience.

Join For Free
This technical tip explains how .NET developers can extract images from presentation shapes inside their .NET applications.  Images are added in slide background and shapes. Sometimes, it is required to extract the images added in the presentation shapes. The images are added in IPPImageCollection inside Presentation Document Object Model (DOM). This article covers the feature of accessing the images in presentation shape, extracting them from presentation collection and saving them in a file. In Aspose.Slides for .NET, images can be added to slide shape and slide background. The images are added in IPPImageCollection of the presentation. In this example we will traverse through each shape inside every slide of presentation and see if there is any image added in slide shape. If the image will be found for any shape, we will extract that and will save it in file. The following code snippet will serve the purpose.
// Code Snippet for Extracting images from presentation shapes

[C# Code Sample]
 
String path = @"D:\Aspose Data\";
//Accessing the presentation
Presentation pres = new Presentation(path + "ExtractImages.pptx");
Aspose.Slides.IPPImage img = null;
Aspose.Slides.IPPImage Backimg = null;

int slideIndex = 0;
String ImageType = "";
bool ifImageFound = false;
for (int i = 0; i < pres.Slides.Count; i++)
{

    slideIndex++;
    //Accessing the first slide
    ISlide sl = pres.Slides[i];
    System.Drawing.Imaging.ImageFormat Format = System.Drawing.Imaging.ImageFormat.Jpeg;

    //Accessing the first slide Slide sl = pres.getSlideByPosition(i);
    if (sl.Background.FillFormat.FillType == FillType.Picture)
    {
        //Getting the back picture  
        Backimg = sl.Background.FillFormat.PictureFillFormat.Picture.Image;

        //Setting the desired picture format 

        ImageType = Backimg.ContentType;
        ImageType = ImageType.Remove(0, ImageType.IndexOf("/") + 1);
        Format = GetImageFormat(ImageType);

        String ImagePath = path + "BackImage_";
        Backimg.SystemImage.Save(ImagePath + "Slide_" + slideIndex.ToString() + "." + ImageType, Format);

    }
    else
    {
        if (sl.LayoutSlide.Background.FillFormat.FillType == FillType.Picture)
        {
            //Getting the back picture  
            Backimg = sl.LayoutSlide.Background.FillFormat.PictureFillFormat.Picture.Image;

            //Setting the desired picture format 

            ImageType = Backimg.ContentType;
            ImageType = ImageType.Remove(0, ImageType.IndexOf("/") + 1);
            Format = GetImageFormat(ImageType);

            String ImagePath = path + "BackImage_Slide_" + i;
            Backimg.SystemImage.Save(ImagePath + "LayoutSlide_" + slideIndex.ToString() + "." + ImageType, Format);

        }
    }

    for (int j = 0; j < sl.Shapes.Count; j++)
    {
        // Accessing the shape with picture
        IShape sh = sl.Shapes[j];

        if (sh is AutoShape)
        {
            AutoShape ashp = (AutoShape)sh;
            if (ashp.FillFormat.FillType == FillType.Picture)
            {
                img = ashp.FillFormat.PictureFillFormat.Picture.Image;
                ImageType = img.ContentType;
                ImageType = ImageType.Remove(0, ImageType.IndexOf("/") + 1);
                ifImageFound = true;

            }
        }

        else if (sh is PictureFrame)
        {
            IPictureFrame pf = (IPictureFrame)sh;
            if (pf.FillFormat.FillType == FillType.Picture)
            {
                img = pf.PictureFormat.Picture.Image;
                ImageType = img.ContentType;
                ImageType = ImageType.Remove(0, ImageType.IndexOf("/") + 1);
                ifImageFound = true;
            }
        }


        //
        //Setting the desired picture format
        if (ifImageFound)
        {
            Format = GetImageFormat(ImageType);
            String ImagePath = path + "Slides\\Image_";
            img.SystemImage.Save(ImagePath + "Slide_" + slideIndex.ToString() + "_Shape_" + j.ToString() + "." + ImageType, Format);
        }
        ifImageFound = false;
    }
}



public static System.Drawing.Imaging.ImageFormat GetImageFormat(String ImageType)
{
    System.Drawing.Imaging.ImageFormat Format = System.Drawing.Imaging.ImageFormat.Jpeg;
    switch (ImageType)
    {
        case "jpeg":
            Format = System.Drawing.Imaging.ImageFormat.Jpeg;
            break;

        case "emf":
            Format = System.Drawing.Imaging.ImageFormat.Emf;
            break;

        case "bmp":
            Format = System.Drawing.Imaging.ImageFormat.Bmp;
            break;

        case "png":
            Format = System.Drawing.Imaging.ImageFormat.Png;
            break;

        case "wmf":
            Format = System.Drawing.Imaging.ImageFormat.Wmf;
            break;

        case "gif":
            Format = System.Drawing.Imaging.ImageFormat.Gif;
            break;

    }
    return Format;


}
 
[VB.NET Code Sample]
 

Dim path As String = "D:\Aspose Data\"
'Accessing the presentation
Dim pres As New Presentation(path & "ExtractImages.pptx")
Dim img As Aspose.Slides.IPPImage = Nothing
Dim Backimg As Aspose.Slides.IPPImage = Nothing

Dim slideIndex As Integer = 0
Dim ImageType As String = ""
Dim ifImageFound As Boolean = False
For i As Integer = 0 To pres.Slides.Count - 1

	slideIndex += 1
	'Accessing the first slide
	Dim sl As ISlide = pres.Slides(i)
	Dim Format As System.Drawing.Imaging.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg

	'Accessing the first slide Slide sl = pres.getSlideByPosition(i);
	If sl.Background.FillFormat.FillType = FillType.Picture Then
		'Getting the back picture  
		Backimg = sl.Background.FillFormat.PictureFillFormat.Picture.Image

		'Setting the desired picture format 

		ImageType = Backimg.ContentType
		ImageType = ImageType.Remove(0, ImageType.IndexOf("/") + 1)
		Format = GetImageFormat(ImageType)

		Dim ImagePath As String = path & "BackImage_"
		Backimg.SystemImage.Save(ImagePath & "Slide_" & slideIndex.ToString() & "." & ImageType, Format)

	Else
		If sl.LayoutSlide.Background.FillFormat.FillType = FillType.Picture Then
'Getting the back picture  
Backimg = sl.LayoutSlide.Background.FillFormat.PictureFillFormat.Picture.Image

'Setting the desired picture format 

ImageType = Backimg.ContentType
ImageType = ImageType.Remove(0, ImageType.IndexOf("/") + 1)
Format = GetImageFormat(ImageType)

Dim ImagePath As String = path & "BackImage_Slide_" & i
Backimg.SystemImage.Save(ImagePath & "LayoutSlide_" & slideIndex.ToString() & "." & ImageType, Format)

		End If
	End If

	For j As Integer = 0 To sl.Shapes.Count - 1
		' Accessing the shape with picture
		Dim sh As IShape = sl.Shapes(j)

		If TypeOf sh Is AutoShape Then
Dim ashp As AutoShape = CType(sh, AutoShape)
If ashp.FillFormat.FillType = FillType.Picture Then
	img = ashp.FillFormat.PictureFillFormat.Picture.Image
	ImageType = img.ContentType
	ImageType = ImageType.Remove(0, ImageType.IndexOf("/") + 1)
	ifImageFound = True

End If

		ElseIf TypeOf sh Is PictureFrame Then
Dim pf As IPictureFrame = CType(sh, IPictureFrame)
If pf.FillFormat.FillType = FillType.Picture Then
	img = pf.PictureFormat.Picture.Image
	ImageType = img.ContentType
	ImageType = ImageType.Remove(0, ImageType.IndexOf("/") + 1)
	ifImageFound = True
End If
		End If


		'
		'Setting the desired picture format
		If ifImageFound Then
Format = GetImageFormat(ImageType)
Dim ImagePath As String = path & "Slides\Image_"
img.SystemImage.Save(ImagePath & "Slide_" & slideIndex.ToString() & "_Shape_" & j.ToString() & "." & ImageType, Format)
		End If
		ifImageFound = False
	Next j
Next i
		End Sub



Public Shared Function GetImageFormat(ByVal ImageType As String) As System.Drawing.Imaging.ImageFormat

	Dim Format As System.Drawing.Imaging.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg

	Select Case ImageType
		Case "jpeg"
			Format = System.Drawing.Imaging.ImageFormat.Jpeg

		Case "emf"
			Format = System.Drawing.Imaging.ImageFormat.Emf

		Case "bmp"
		Format = System.Drawing.Imaging.ImageFormat.Bmp

		Case "png"
			Format = System.Drawing.Imaging.ImageFormat.Png

		Case "wmf"
			Format = System.Drawing.Imaging.ImageFormat.Wmf

		Case "gif"
			Format = System.Drawing.Imaging.ImageFormat.Gif

	End Select

	Return Format

End Function

Extract

Opinions expressed by DZone contributors are their own.

Related

  • Python Techniques for Text Extraction From Images
  • OneStream Fast Data Extracts APIs
  • Reversing an Array: An Exploration of Array Manipulation
  • Setup ActiveMQ Artemis on Windows

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!