We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Problem adding WMF images

Hi,

I have a problem when adding WMF images to a Slide which already contains the WMF. I get empty boxes with a red X that read “This image cannot currently be displayed”.

The slide contains multiple instances of the WMF image (which is used as a nice frame around an underlying photo).
When I try to replace an instance of the WMF image, it only works if the images are different:
If I’m trying to replace one frame with the same frame (I do this to adjust the size), it doesn’t work.
If I’m replacing one frame with a different frame, it works OK.

What am I doing wrong?

The code:

Protected Function AddFrame(ByVal SlideNum As Integer, ByVal ShapeName As String, ByVal FrameID As Integer) As Boolean
'ShapeName = “ItemPass1” - the ShapeName of the frame of this photo is “ItemPass1Frame”
        Try
Dim slide As Slide = _pres.GetSlideByPosition(SlideNum)
Dim RelShape As Aspose.Slides.Shape = FindShape(slide, ShapeName)
Dim frame As New Frame
If Not RelShape Is Nothing Then
RelShape.LineFormat.ShowLines = False
Dim NewPic As String = “”
If FrameID <> 0 Then
'get the filename from DB
frame.FrameID = FrameID
If frame.GetFrameData Then
                        If RelShape.Height >= RelShape.Width Then
NewPic = frame.FrameFrameT
Else
NewPic = frame.FrameFrameW
End If
                    <span style="color:blue;">If</span> <span style="color:blue;">Not</span> FindShape(slide, ShapeName & <span style="color:#a31515;">"Frame"</span>) <span style="color:blue;">Is</span> <span style="color:blue;">Nothing</span> <font class="Apple-style-span" color="#0000ff">Then

'remove previous frame
Dim RelShapeFrame As Aspose.Slides.Shape = FindShape(slide, ShapeName & “Frame”)
slide.Shapes.Remove(RelShapeFrame)
End If

                    <span style="color:blue;">Dim</span> pic <span style="color:blue;">As</span> <span style="color:#2b91af;">Picture</span> = <span style="color:blue;">New</span> <span style="color:#2b91af;">Picture</span>(_pres, <span style="color:#2b91af;">HttpContext</span>.Current.Server.MapPath(<span style="color:#a31515;">"~\"</span> & _GlobalPath.FramePath().ToString & <span style="color:#a31515;">"\"</span> & NewPic))
                    <span style="color:blue;">Dim</span> PictureData <span style="color:blue;">As</span> <span style="color:blue;">New</span> <span style="color:#2b91af;">PictureData</span>
                    PictureData.PicID = _pres.Pictures.Add(pic)
                    <span style="color:blue;">Dim</span> Cord() <span style="color:blue;">As</span> <span style="color:blue;">String</span> = Split(NewPic, <span style="color:#a31515;">"_"</span>)
                    <span style="color:blue;">Dim</span> TempCord() <span style="color:blue;">As</span> <span style="color:blue;">String</span> = Split(Cord(4), <span style="color:#a31515;">"."</span>)
                    Cord(4) = TempCord(0)
                    <span style="color:blue;">Dim</span> R(3) <span style="color:blue;">As</span> <span style="color:blue;">Double</span>

                    R(0) = <span style="color:#2b91af;">Convert</span>.ToInt32(Cord(1)) * 0.001
                    R(1) = <span style="color:#2b91af;">Convert</span>.ToInt32(Cord(2)) * 0.001
                    R(2) = <span style="color:#2b91af;">Convert</span>.ToInt32(Cord(3)) * 0.001
                    R(3) = <span style="color:#2b91af;">Convert</span>.ToInt32(Cord(4)) * 0.001

                    <span style="color:blue;">Dim</span> x <span style="color:blue;">As</span> <span style="color:blue;">Integer</span> = RelShape.X - <span style="color:#2b91af;">Math</span>.Abs(RelShape.Width * R(0))
                    <span style="color:blue;">Dim</span> y <span style="color:blue;">As</span> <span style="color:blue;">Integer</span> = RelShape.Y - <span style="color:#2b91af;">Math</span>.Abs(RelShape.Height * R(1))
                    <span style="color:blue;">Dim</span> width <span style="color:blue;">As</span> <span style="color:blue;">Integer</span> = RelShape.Width + (RelShape.Width * (R(0) + R(2)))
                    <span style="color:blue;">Dim</span> height <span style="color:blue;">As</span> <span style="color:blue;">Integer</span> = RelShape.Height + (RelShape.Height * (R(1) + R(3)))

                    <span style="color:blue;">Dim</span> shape <span style="color:blue;">As</span> Aspose.Slides.<span style="color:#2b91af;">Shape</span> = slide.Shapes.AddPictureFrame(PictureData.PicID, x, y, width, height)
                    shape.Name = ShapeName & <span style="color:#a31515;">"Frame"</span>
                    shape.AlternativeText = FrameID

                    <span style="color:blue;">Return</span> <span style="color:blue;">True</span>
                <span style="color:blue;">Else</span>
                    WriteLog(<span style="color:#a31515;">"AddFrame - Ex FrameID"</span> & FrameID & vbCrLf & _
                        <span style="color:#a31515;">"AddFrame - Ex ShapeName "</span> & ShapeName & vbCrLf & _
                        <span style="color:#a31515;">"AddFrame - Ex SlideNum "</span> & SlideNum & vbCrLf, <span style="color:#a31515;">"Error"</span>)
                    <span style="color:blue;">Return</span> <span style="color:blue;">False</span>
                <span style="color:blue;">End</span> <span style="color:blue;">If</span>
            <span style="color:blue;">Else</span>
                <span style="color:blue;">Dim</span> RelShapeFrame <span style="color:blue;">As</span> Aspose.Slides.<span style="color:#2b91af;">Shape</span> = FindShape(slide, ShapeName & <span style="color:#a31515;">"Frame"</span>)
                slide.Shapes.Remove(RelShapeFrame)

                <span style="color:blue;">Return</span> <span style="color:blue;">False</span>
            <span style="color:blue;">End</span> <span style="color:blue;">If</span>
        <span style="color:blue;">End</span> <span style="color:blue;">If</span>
    <span style="color:blue;">Catch</span> ex <span style="color:blue;">As</span> <span style="color:#2b91af;">Exception</span>
        WriteLog(<span style="color:#a31515;">"AddFrame - Ex FrameID"</span> & FrameID & vbCrLf & _
                    <span style="color:#a31515;">"AddFrame - Ex ShapeName "</span> & ShapeName & vbCrLf & _
                    <span style="color:#a31515;">"AddFrame - Ex SlideNum "</span> & SlideNum & vbCrLf & _
                    ex.ToString, <span style="color:#a31515;">"Error"</span>)
        <span style="color:blue;">Return</span> <span style="color:blue;">False</span>
    <span style="color:blue;">End</span> <span style="color:blue;">Try</span>
<span style="color:blue;">End</span> <span style="color:blue;">Function</span></pre></div><div>Example of WMF filenames I use:</div><div><span class="Apple-style-span" style="font-family: Verdana, Helvetica; font-size: 13px; background-color: rgb(255, 255, 255); ">M006t_147_68_147_68.WMF</span></div><div><span class="Apple-style-span" style="font-family: Verdana, Helvetica; font-size: 13px; background-color: rgb(255, 255, 255); ">M006w_65_150_63_141.WMF</span></div><div><span class="Apple-style-span" style="font-family: Verdana, Helvetica; font-size: 13px; background-color: rgb(255, 255, 255); "><br></span></div><div><span class="Apple-style-span" style="font-family: Verdana, Helvetica; font-size: 13px; background-color: rgb(255, 255, 255); ">P.S.</span></div><div><span class="Apple-style-span" style="font-family: Verdana, Helvetica; font-size: 13px; background-color: rgb(255, 255, 255); ">If I save the Thumbnail of the slide, the WMF frames appear OK, even if saving the slide as PPT might give red X's.</span></div><div><font class="Apple-style-span" face="Verdana, Helvetica" size="2">Also, I tried updating Aspose.Slides to version 5.9, but the problem persists.</font></div><div><span class="Apple-style-span" style="font-family: Verdana, Helvetica; font-size: 13px; background-color: rgb(255, 255, 255); "><br></span></div><div><span class="Apple-style-span" style="font-family: Verdana, Helvetica; font-size: 13px; background-color: rgb(255, 255, 255); ">Thanks,</span></div><div><span class="Apple-style-span" style="font-family: Verdana, Helvetica; font-size: 13px; background-color: rgb(255, 255, 255); ">Yanai.</span></div>

Hi Yanai,


Thanks for your interest in Aspose.Slides.

I have observed the code snippet shared by you and like to share that I am not able to use that on my end owing to some missing information like Frame and PictureData classes. I would suggest you to kindly share a complete working project code so that I may try reproducing the same on my end. I would also suggest you to please try using Aspose.Slides for .NET 6.0.0 as well, which is latest available version. Moreover, the images shared by you failed to open on my end. Please share them again as well.

Many Thanks,

Hi,

Sharing the entire project would be too complex, as this piece of code is a part of a large website.
I have revised the code to make it easier to use independently. Please see below.

The pictures I uploaded are WMF images, but I had to add a .JPG extension since this forum’s uploader didn’t accept the WMF files.

I have now tried Slides 6.0, but I’m afraid it didn’t help either.

Thanks,
Yanai.

Code:
note: _pres is a Presentation object.
Protected Function AddFrame(ByVal SlideNum As Integer, ByVal ShapeName As String, filename As String) As Boolean
Try
Dim slide As Slide = _pres.GetSlideByPosition(SlideNum)
Dim RelShape As Aspose.Slides.Shape = FindShape(slide, ShapeName)
If Not RelShape Is Nothing Then
RelShape.LineFormat.ShowLines = False
Dim NewPic As String = “”
NewPic = filename
        <span style="color:blue;">If</span> <span style="color:blue;">Not</span> FindShape(slide, ShapeName & <span style="color:#a31515;">"Frame"</span>) <span style="color:blue;">Is</span> <span style="color:blue;">Nothing</span> <span style="color:blue;">Then</span>
            <span style="color:green;">'remove previous frame</span>
            <span style="color:blue;">Dim</span> RelShapeFrame <span style="color:blue;">As</span> Aspose.Slides.Shape = FindShape(slide, ShapeName & <span style="color:#a31515;">"Frame"</span>)
            slide.Shapes.Remove(RelShapeFrame)
        <span style="color:blue;">End</span> <span style="color:blue;">If</span>

        <span style="color:blue;">Dim</span> pic <span style="color:blue;">As</span> Picture = <span style="color:blue;">New</span> Picture(_pres, HttpContext.Current.Server.MapPath(<span style="color:#a31515;">"~\"</span> & NewPic))
        <span style="color:blue;">Dim</span> PicID <span style="color:blue;">As</span> <span style="color:blue;">Integer</span> = _pres.Pictures.Add(pic)
        <span style="color:blue;">Dim</span> Cord() <span style="color:blue;">As</span> <span style="color:blue;">String</span> = Split(NewPic, <span style="color:#a31515;">"_"</span>)
        <span style="color:blue;">Dim</span> TempCord() <span style="color:blue;">As</span> <span style="color:blue;">String</span> = Split(Cord(4), <span style="color:#a31515;">"."</span>)
        Cord(4) = TempCord(0)
        <span style="color:blue;">Dim</span> R(3) <span style="color:blue;">As</span> <span style="color:blue;">Double</span>

        R(0) = Convert.ToInt32(Cord(1)) * 0.001
        R(1) = Convert.ToInt32(Cord(2)) * 0.001
        R(2) = Convert.ToInt32(Cord(3)) * 0.001
        R(3) = Convert.ToInt32(Cord(4)) * 0.001

        <span style="color:blue;">Dim</span> x <span style="color:blue;">As</span> <span style="color:blue;">Integer</span> = RelShape.X - Math.Abs(RelShape.Width * R(0))
        <span style="color:blue;">Dim</span> y <span style="color:blue;">As</span> <span style="color:blue;">Integer</span> = RelShape.Y - Math.Abs(RelShape.Height * R(1))
        <span style="color:blue;">Dim</span> width <span style="color:blue;">As</span> <span style="color:blue;">Integer</span> = RelShape.Width + (RelShape.Width * (R(0) + R(2)))
        <span style="color:blue;">Dim</span> height <span style="color:blue;">As</span> <span style="color:blue;">Integer</span> = RelShape.Height + (RelShape.Height * (R(1) + R(3)))

        <span style="color:blue;">Dim</span> shape <span style="color:blue;">As</span> Aspose.Slides.Shape = slide.Shapes.AddPictureFrame(PicID, x, y, width, height)
        shape.Name = ShapeName & <span style="color:#a31515;">"Frame"</span>
        shape.AlternativeText = filename

        <span style="color:blue;">Return</span> <span style="color:blue;">True</span>
    <span style="color:blue;">Else</span>
        <span style="color:blue;">Throw</span> <span style="color:blue;">New</span> Exception
        <span style="color:blue;">Return</span> <span style="color:blue;">False</span>
    <span style="color:blue;">End</span> <span style="color:blue;">If</span>
<span style="color:blue;">Catch</span> ex <span style="color:blue;">As</span> Exception
    <span style="color:blue;">Return</span> <span style="color:blue;">False</span>
<span style="color:blue;">End</span> <span style="color:blue;">Try</span>

End Function

Private Function FindShape(ByVal slide As Slide, ByVal ShapeName As String) As Aspose.Slides.Shape
Try
Dim i As Integer
For i = 0 To slide.Shapes.Count - 1
If slide.Shapes(i).Name.CompareTo(ShapeName) = 0 Then
Return slide.Shapes(i)
End If
Next

    <span style="color:blue;">Return</span> <span style="color:blue;">Nothing</span>
<span style="color:blue;">Catch</span> ex <span style="color:blue;">As</span> Exception

    <span style="color:blue;">Return</span> <span style="color:blue;">Nothing</span>
<span style="color:blue;">End</span> <span style="color:blue;">Try</span>

End Function

Hi Yanai,


I have been able to execute the code snippet shared by you. Actually, you are loading the WMF file as normal JPEG, BMP or PNG file. This is where you are having issue. You need to load the WMF file as Metafile and then add in presentation picture collection as Metafile. Doing, so will resolve the issue. I have been not observed the issue once doing so. The modified code snippet along with generated presentation is shared for your kind reference. Please share, if I may help you further in this regard.

Public Function AddFrame(ByVal SlideNum As Integer, ByVal ShapeName As String, ByVal FrameID As Integer, ByRef _pres As Presentation) As Boolean
’ Protected Function AddFrame(ByVal SlideNum As Integer, ByVal ShapeName As String, ByVal filename As String) As Boolean
Try
Dim slide As Slide = _pres.GetSlideByPosition(SlideNum)
Dim RelShape As Aspose.Slides.Shape = FindShape(slide, ShapeName)
If Not RelShape Is Nothing Then
RelShape.LineFormat.ShowLines = False
Dim NewPic As String = “”
Dim filename As String = “M006t_147_68_147_68.WMF”
'Dim filename As String = “M006t_147_68_147_68.jpg”
NewPic = filename

If Not FindShape(slide, ShapeName & “Frame”) Is Nothing Then
'remove previous frame
Dim RelShapeFrame As Aspose.Slides.Shape = FindShape(slide, ShapeName & “Frame”)
slide.Shapes.Remove(RelShapeFrame)
End If

Dim MF As System.Drawing.Imaging.Metafile = New System.Drawing.Imaging.Metafile(“D:” + filename)
'Dim pic As Picture = New Picture(_pres, HttpContext.Current.Server.MapPath("~" & NewPic))
Dim pic As Picture = New Picture(_pres, MF)

’ Dim pic As Picture = New Picture(_pres, “D:” + filename)
Dim PicID As Integer = pres.Pictures.Add(pic)
Dim Cord() As String = Split(NewPic, "")
Dim TempCord() As String = Split(Cord(4), “.”)
Cord(4) = TempCord(0)
Dim R(3) As Double

R(0) = Convert.ToInt32(Cord(1)) * 0.001
R(1) = Convert.ToInt32(Cord(2)) * 0.001
R(2) = Convert.ToInt32(Cord(3)) * 0.001
R(3) = Convert.ToInt32(Cord(4)) * 0.001

Dim x As Integer = RelShape.X - Math.Abs(RelShape.Width * R(0))
Dim y As Integer = RelShape.Y - Math.Abs(RelShape.Height * R(1))
Dim width As Integer = RelShape.Width + (RelShape.Width * (R(0) + R(2)))
Dim height As Integer = RelShape.Height + (RelShape.Height * (R(1) + R(3)))

Dim shape As Aspose.Slides.Shape = slide.Shapes.AddPictureFrame(PicID, x, y, width, height)
shape.Name = ShapeName & “Frame”
shape.AlternativeText = filename

Return True
Else
Throw New Exception
Return False
End If
Catch ex As Exception
Return False
End Try
End Function

Private Function FindShape(ByVal slide As Slide, ByVal ShapeName As String) As Aspose.Slides.Shape
Try
Dim i As Integer
For i = 0 To Slide.Shapes.Count - 1
If Slide.Shapes(i).Name.CompareTo(ShapeName) = 0 Then
Return Slide.Shapes(i)
End If
Next

Return Nothing
Catch ex As Exception

Return Nothing
End Try
End Function


Many Thanks,