none
PowerPoint - How to access a Picture's color information RRS feed

  • Question

  • Question: In PowerPoint 2007, how to access the color information about a picture on a slide?

    Background:

    I am using PowerPoint 2007 and Visual Studio.  I have a picture on a slide and I want to be able to read the colors present in the picture to see if they conform to some rules I have.  

    Problem:

    I know I can find the shape (Type = msoPicture) but I cannot seem to get any information about the picture itself.  I can do the basic interop operations using the .PictureFormat property but how to get at the picture itself?  Perhaps a better question would be how to get said picture accessible by System.Drawing.

    Hoping something can help.

    Thursday, April 14, 2011 8:24 AM

Answers

  • To close this thread in case other people find it ...

    How to recolor a piece of ClipArt that is in WMF or EMF format:

      Public Shared Sub recolorClipArt()
        Dim shp As PowerPoint.Shape = Nothing
        Dim grpShp As PowerPoint.ShapeRange = Nothing
    
        Try
          If Selection.IsImageSelected() Then
            shp = Globals.pptApp.ActiveWindow.Selection.ShapeRange(1)
            grpShp = shp.Ungroup()
          Else
            grpShp = Globals.pptApp.ActiveWindow.Selection.ShapeRange
          End If
    
          For Each subShp As PowerPoint.Shape In grpShp.GroupItems
            If subShp.Fill.ForeColor.RGB = RGB(0, 0, 0) Then
              subShp.Fill.ForeColor.RGB = RGB(255, 255, 255)
            End If
          Next
    
        Catch ex As Exception
        End Try
      End Sub
    
    Basically only reasonable solution appears to be to Ungroup it first where-upon it becomes native PPT objects that can be manipulated without loss of quality.

    • Marked as answer by Scytale Wednesday, May 25, 2011 3:04 PM
    Wednesday, May 25, 2011 3:04 PM

All replies

  • Hi Scyntale

    Probably, you'd need to either copy it to the Clipboard or save it to a file before any .NET tools can delve into the actual graphic.

    If the persentation were not open in PowerPoint then you could extract the graphics from the pptx file, directly, using Open XML (see OpenXMLDeveloper.org and the Open XML SDK forum).

    You migiht want to ask the PowerPoint specialists, however, whether they know of any other approach. You'll find them in the end-user forums on the Answers site, even the developers...


    Cindy Meister, VSTO/Word MVP
    Thursday, April 14, 2011 12:47 PM
    Moderator
  • At the moment it is maddening.

    Dim shp As PowerPoint.Shape = Globals.pptApp.ActiveWindow.Selection.ShapeRange(1)
    
    shp.Copy()
    
    Dim img As System.Drawing.Image = Clipboard.GetImage()
    
    

    This appears to ruin the quality of the image.

     

    Thursday, April 14, 2011 2:43 PM
  • Dear Scytale,

    What you you mean about ruin the qulity of the image?

    I think it better to use Open XML SDK to retrive the image as Cindy suggested, here is a article about manipulate image in powerpint slide: http://msdn.microsoft.com/en-us/library/bb332455(office.12).aspx.

     Then, you can get more details of the image via C#

    Hope this helps.

    Regards,


    Be happy.
    Thursday, April 21, 2011 5:18 AM
  • Hi Scytale,

    Have you resolved your problem yet and does the suggestion help you? If you still have any concern on the thread, feel free to follow up. 

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, April 25, 2011 4:04 AM
  • Hi,

    another approach...

    using System.Linq;
    using Microsoft.Office.Interop.PowerPoint;
    using System.Drawing;
    using PowerPoint = Microsoft.Office.Interop.PowerPoint;
    using Microsoft.Office.Tools.Ribbon;
    
    namespace PowerPointAddIn1 {
      public partial class Ribbon1 {
        private void Ribbon1_Load(object sender, RibbonUIEventArgs e) {
    
        }
    
        private void button1_Click(object sender, RibbonControlEventArgs e) {
          PowerPoint.Shape shape = Globals.ThisAddIn.Application.ActiveWindow.Selection.ShapeRange[1];
          shape.Export("C:\\test.png", PpShapeFormat.ppShapeFormatPNG, 0, 0, PpExportMode.ppScaleXY);
    
          Image img = Image.FromFile("C:\\test.png");
    
          if (img.Palette.Entries.Contains(Color.Red)) {
            // TODO Something...
          }
    
    
        }
      }
    }
    

    Greets - Helmut

     


    Helmut Obertanner [http://www.x4u.de] [http://www.outlooksharp.de]
    Tuesday, April 26, 2011 6:22 AM
    Answerer
  • Thank you for the responses but so far I have not managed to resolve the problem.

    I have a PNG in PowerPoint which I wish to manipulate.  For example: MS ClipArt

     

    Dim shp As PowerPoint.Shape = Globals.pptApp.ActiveWindow.Selection.ShapeRange(1)
    
    ' ##### From clipboard
    shp.Copy()
    Dim img As System.Drawing.Image = Clipboard.GetImage
    Dim gfx As Graphics = Graphics.FromImage(img)
    

    When I use the above code then the "GetImage" provides a bitmap which ruins the quality, i.e. the PNG is rendered as a Bitmap which blurs the outline of all the edges.

    If I try to export / import from file as PNG then the quality is still reduced ...

    Dim shp As PowerPoint.Shape = Globals.pptApp.ActiveWindow.Selection.ShapeRange(1)
    
    ' ##### From file
    shp.Export("D:\temp\original.png", PpShapeFormat.ppShapeFormatPNG)
    Dim img As System.Drawing.Image = Image.FromFile("D:\temp\original.png")
    Dim gfx As Graphics = Graphics.FromImage(img)
    
    ' Do something
    
    img.Save("D:\temp\original2.png", System.Drawing.Imaging.ImageFormat.Png)
    Globals.pptApp.ActivePresentation.Slides(1).Shapes.AddPicture("D:\temp\original2.png", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, shp.Top, shp.Left)
    
    
    I cannot seem to find a way by which the image quality stays the same.
    Tuesday, April 26, 2011 11:26 AM
  • I have been able to successfully export / import the image in either EMF or WMF format without any loss of quality but when trying to ...

     

    Dim gfx As Graphics = Graphics.FromImage(img)
    

     

    ... it gives a ...

     

    A first chance exception of type 'System.OutOfMemoryException' occurred in System.Drawing.dll
    Tuesday, April 26, 2011 11:42 AM
  • Dear Scytale,

    Dim img As System.Drawing.Image = Clipboard.GetImage
    Dim gfx As Graphics = Graphics.FromImage(img)

    I think you didn't get the image from the clipborad, you can check whether img object is nothing. I have ever remembered that Office has its own mechanism of clipboard, we can't directly get image from clipboard.

    So, my suggestion is that you can extract this image via openxml sdk or Office PIA not using clipboard.

    Hope this suggestion can help you.

    Best Regards,


    Be happy.
    Thursday, April 28, 2011 9:30 AM
  • So, my suggestion is that you can extract this image via openxml sdk or Office PIA not using clipboard.


    Hi Mike, using the Office PIA, as per my second code example, has the problem that either the EMF or WMF image cannot be loaded into the gfx variable or a PNG can be loaded but the picture loses quality.


    Thursday, April 28, 2011 10:18 AM
  • Hi Scytale,

    From the exception, take a look at this thread: http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/4aac43fa-cccb-4bf7-b37e-58ec5351ab80/, see whether it can help you, it says that the images may be closed too soon.

    Try add using block to see whether it helps.

    Best Wishes,


    • Edited by KittyFace Tuesday, May 3, 2011 7:34 AM typo
    Tuesday, May 3, 2011 7:32 AM
  • Hi KittyFace,

    I did review this article and sadly it did not help.  I think the fundamental problem is that the Graphics object will not accept a EMF or WMF picture.  

    So I am totally stuck as to how to manipulate a ClipArt in PowerPoint through Interop - manipulate more than the basic functions PowerPoint provides.

     

    Tuesday, May 3, 2011 8:31 AM
  • Hi Scytale,

    Can you save the EMF picture to the local drive and then convert it to other formats? You can use the converter: http://www.coolutils.com/Formats/WMF . After converting, you can load it from file:

    Graphics.FromImage("coverted.jpg");

    Hope this can help you.

    Best Wishes

    Wednesday, May 4, 2011 7:08 AM
  • The Coolutils tool is command line though.  This is not exactly programmatic.

     

    This still begs the fundamental question: How to manipulate a ClipArt image (EMF or WMF format), through code, whilst retaining the quality.

    Wednesday, May 25, 2011 2:23 PM
  • To close this thread in case other people find it ...

    How to recolor a piece of ClipArt that is in WMF or EMF format:

      Public Shared Sub recolorClipArt()
        Dim shp As PowerPoint.Shape = Nothing
        Dim grpShp As PowerPoint.ShapeRange = Nothing
    
        Try
          If Selection.IsImageSelected() Then
            shp = Globals.pptApp.ActiveWindow.Selection.ShapeRange(1)
            grpShp = shp.Ungroup()
          Else
            grpShp = Globals.pptApp.ActiveWindow.Selection.ShapeRange
          End If
    
          For Each subShp As PowerPoint.Shape In grpShp.GroupItems
            If subShp.Fill.ForeColor.RGB = RGB(0, 0, 0) Then
              subShp.Fill.ForeColor.RGB = RGB(255, 255, 255)
            End If
          Next
    
        Catch ex As Exception
        End Try
      End Sub
    
    Basically only reasonable solution appears to be to Ungroup it first where-upon it becomes native PPT objects that can be manipulated without loss of quality.

    • Marked as answer by Scytale Wednesday, May 25, 2011 3:04 PM
    Wednesday, May 25, 2011 3:04 PM
  • Thanks for taking a minute to follow up with this :-)
    Cindy Meister, VSTO/Word MVP
    Wednesday, May 25, 2011 4:28 PM
    Moderator