A generic error occurred in GDI+ in win7 and win server 2008 r2

Hi Aspose,
When I loaded and processed attached ppt using Aspose Slide, it threw exception.
I attached sample.zip and this worked fine with MS power point. I used Aspose Slide(16.12.0).
Make sure “C:\Windows\Temp” exist. Tested in win7 and win server 2008 r2. I used dot net frame work 4.5
My samples are INFECTED PPT.
My sample code:
static void Main(string[] args)
{
string inputPptFile = args[0];
int imageIndex = 0;
using (Presentation presentation = new Presentation(inputPptFile))
{
try
{
foreach (IPPImage tmp in presentation.Images)
{
string ImageType = “”;
ImageType = tmp.ContentType;
ImageType = ImageType.Remove(0, ImageType.IndexOf("/") + 1);
ImageFormat Format = GetImageFormat(ImageType);
string target = “C:\Windows\Temp” + “\temp” + imageIndex.ToString() + “.” + ImageType;
tmp.SystemImage.Save(target, Format); // Exception here
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
public static 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 “bmp”:
Format = System.Drawing.Imaging.ImageFormat.Bmp;
break;
case “png”:
Format = System.Drawing.Imaging.ImageFormat.Png;
break;
case “gif”:
Format = System.Drawing.Imaging.ImageFormat.Gif;
break;
}
return Format;
}
}
}

Exception message is "A generic error occurred in GDI+"
Note:
- I tried solution in this post but no good:
- If i deleted first image in sample 36b7fc2085704b99aec5ad02a129d7c9.ppt, it works.

Hi,

I have observed the issue shared by you and have been able to reproduce the issue specified. An issue with ID SLIDESNET-38295 has been created in our issue tracking system to further investigate and resolve the issue. This thread has been linked with the issue so that you may be automatically notified once the issue will be fixed.

We are sorry for your inconvenience,

Hi,


We have investigated the issue on our end and suggest you to please try using following sample code. Actually, you may not use SystemImage to save the image, but use BinaryData property instead. It will be faster and does not require any GDI-specific operations (when native .NET image using to resave the JPEG)

    public static void TestPresExport()
{
String path=@“C:\Aspose Data\sample”;
string inputPptFile =path+“6.ppt”;
int imageIndex = 0;
using (Presentation presentation = new Presentation(inputPptFile))
{
try
{
foreach (IPPImage tmp in presentation.Images)
{
string ImageType = “”;
ImageType = tmp.ContentType;
ImageType = ImageType.Remove(0, ImageType.IndexOf("/") + 1);
ImageFormat Format = GetImageFormat(ImageType);
// string target = “C:\Windows\Temp” + “\temp” + imageIndex.ToString() + “.” + ImageType;
string target = path + “temp” + imageIndex.ToString() + “.” + ImageType;
try
{
// ====> CHANGES ARE HERE
System.IO.File.WriteAllBytes(target, tmp.BinaryData);
                        <span class="code-comment" style="color: rgb(128, 128, 128);">//tmp.SystemImage.Save(target, Format); // Exception here

}
catch(Exception ex)
{
Console.WriteLine(ex.StackTrace);

                    }
                }
            }
            <span class="code-keyword" style="color: rgb(0, 0, 145);">catch</span> (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }</pre><pre class="code-java" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; font-size: 13px; line-height: 1.3em; max-height: 30em; overflow: auto; word-wrap: normal;"><br></pre><pre class="code-java" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; font-size: 13px; line-height: 1.3em; max-height: 30em; overflow: auto; word-wrap: normal;"><font face="Arial">Many Thanks,</font></pre></div>

Hi Aspose team,


I got the same error “A generic error occurred in GDI+” when using function ReplaceImage to replace image in PPT files on Windows 7 64bit and Aspose Slide for .NET 17.3. Take your code above as example:

	public static void TestPresExport()
{
String path=@“C:\Aspose Data\sample”;
string inputPptFile =path+“6.ppt”;
int imageIndex = 0;
using (Presentation presentation = new Presentation(inputPptFile))
{
try
{
foreach (IPPImage tmp in presentation.Images)
{
string ImageType = “”;
ImageType = tmp.ContentType;
ImageType = ImageType.Remove(0, ImageType.IndexOf("/") + 1);
ImageFormat Format = GetImageFormat(ImageType);
// string target = “C:\Windows\Temp” + “\temp” + imageIndex.ToString() + “.” + ImageType;
string target = path + “temp” + imageIndex.ToString() + “.” + ImageType;
try
{
System.IO.File.WriteAllBytes(target, tmp.BinaryData);
                        <span class="code-comment" style="color: rgb(128, 128, 128);">tmp.ReplaceImage(target); // Exception here

}
catch(Exception ex)
{
Console.WriteLine(ex.StackTrace);

                    }
                }
            }
            <span class="code-keyword" style="color: rgb(0, 0, 145);">catch</span> (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }<br></pre><div><br>Please help me. Thanks</div></div>

Hi,


I have observed your comments. Can you please share source presentation so that we can further investigate to help you out.

Best Regards,

Hi,

Thanks for inquiring Aspose.Slides.

I have observed your sample code and like to share that there is issue in your sample code when using ReplaceImage() method. You are passing string as argument of function which is wrong. Please try using following sample code on your end to serve the purpose.

public static void TestPresExport2()
{
String path = @“C:\Aspose Data”;
string inputPptFile = path + “ImageChange.pptx”;
int imageIndex = 0;
using (Presentation presentation = new Presentation(inputPptFile))
{
try
{
foreach (IPPImage tmp in presentation.Images)
{
string ImageType = “”;
ImageType = tmp.ContentType;
ImageType = ImageType.Remove(0, ImageType.IndexOf("/") + 1);
ImageFormat Format = GetImageFormat(ImageType);
// string target = “C:\Windows\Temp” + “\temp” + imageIndex.ToString() + “.” + ImageType;
string target = path + “temp” + imageIndex.ToString() + “.” + ImageType;
Image newImage = Image.FromFile(path + “Penguins.jpg”);
try
{
System.IO.File.WriteAllBytes(target, tmp.BinaryData);

// tmp.ReplaceImage(target); // Exception here
tmp.ReplaceImage(newImage); // Exception here


}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);

}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
presentation.Save(path + “SavedPres.pptx”, SaveFormat.Pptx);
}
}


Many Thanks,

Hi Aspose team,
Thanks for your reply.
I have tested again with your solution and it still throw error.
I want to export image from PPT file, do some stuff with it and then import back. Please check my new code

public static void TestPresExport2()
{
String path = @“C:\Aspose Data”;
string inputPptFile = path + “ImageChange.pptx”;
int imageIndex = 0;
using (Presentation presentation = new Presentation(inputPptFile))
{
try
{
foreach (IPPImage tmp in presentation.Images)
{
string ImageType = “”;
ImageType = tmp.ContentType;
ImageType = ImageType.Remove(0, ImageType.IndexOf("/") + 1);
ImageFormat Format = GetImageFormat(ImageType);
// string target = “C:\Windows\Temp” + “\temp” + imageIndex.ToString() + “.” + ImageType;
string target = path + “temp” + imageIndex.ToString() + “.” + ImageType;
try
{
System.IO.File.WriteAllBytes(target, tmp.BinaryData);

// Do some stuff with exported image (target)

// Then import back to PPT
Image newImage = Image.FromFile(target);
tmp.ReplaceImage(newImage); // <-- Still throw exception :frowning:
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);

}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
presentation.Save(path + “SavedPres.pptx”, SaveFormat.Pptx);
}
}


Hi,

Thank you for sharing the feedback.

Can you please provide the source presentation and image file that you are using on your end so that I may work on that to help you further. Actually, the code sample, presentation and image that I shared with you earlier were perfectly working on my end.

Many Thanks,

Hi,

This is my sample file. It’s infected so do not open.
Noted that I just want to export image and import back that image to PPT.
Thanks

Hi,

I have observed your following message.

"This is my sample file. It’s infected so do not open."

Is this infected with some virus? If I am unable to open this using PowerPoint then Aspose.Slides will not be able to open that as well. Can you please elaborate what did you mean by your above message.

Many Thanks,

Hi,

Sorry, my mistake. My sample file is totally clean and you can open it by PowerPoint.

Hi,

Than you for your elaboration. I have attached a working sample project for your convenience that is extracting the image and sharing that to file. You can also hold the same object in memory as well inside Image object if you like to. You can then modify the image and then load that again and set that as image of picture frame. It works without any issue or exception on my end with your presentation using Aspose.Slides for .NET 17.3.0. I hope the shared project will be helpful.

Many Thanks,

Hi,

Thanks for your reply, I will check it

Hi,

I have tested your ReplaceImage project with the newest version of Aspose Slide 17.3.0 on Windows 7 64 bit and the error still occur.
Please check and test on both Windows 7 64 bit and Windows server 2008 r2
Thanks

Hi,

Thanks for sharing the feedback. I like to share that I have generated the solution in Windows 7 x64 environment on my end and it worked without any issue. I will also try arranging Windows Serve 2008 R2 as well for the test. Can you please provide the complete stacktrace of issue you are having on your end. Moreover, I also suggest you to please verify the file writing permission on your end as in past, I encountered GDI exception while generating slide thumbnails and writing image to a directory for which I had no rights to write file. It may also be a reason and I suggest you to please check this as well and try logging in with administrator and try sample project that I have shared with you.

Many Thanks,

Hi,

This is error stacktrace on my machine:

Failed to replace image. at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams)
at Aspose.Slides.PPImage.ReplaceImage(Image newImage)
at ReplaceImage.Program.TestPresExport()

I run the project in Administrator mode and the directory used for writing image has writing permission.
I found that if we remove all images of the first slide of infected.ppt then it works.
My windows version is Windows 7 Ultimate Service Pack 1 64 bit.
Thanks

Hi,

Thank you for sharing the feedback. I have been able to observe the issue in first image of slide 1 when using application in Windows Server 2008 R2. An issue with ID SLIDESNET-38587 has been created in our issue tracking system to further investigate and resolve the issue. This thread has been linked with the issue so that you may be automatically notified once the issue will be fixed. I also suggest you to please add following line inside for loop of application where you are extracting image.

imageIndex++;

We are sorry for your inconvenience,

Hi Aspose team,
Do we have any update for this bug?

@kto,

I have verified the issue status from our issue tracking system and regret to share that at present the issue is still unresolved. I have requested our product team to share further updates in this regard and will share the feedback with you as soon as possible.

@kto,

Our product team has investigated the issue on their end. After investigating the issue, we have found out that the cause of the issue is an infamous issue occurring in time of using System.Drawing.Image.Save method.

Now we have a solution of this issue. Please take a look at the code snippet. I hope this will help.

   private static Image FixImage(Image image)
    {
        Image fixedImage = null;

        //Convert current image to Bmp format
        MemoryStream memStreamOfBmp = new MemoryStream();
        image.Save(memStreamOfBmp, ImageFormat.Bmp);

        //Convert from Bmp format to the actual format
        fixedImage = Image.FromStream(memStreamOfBmp);
        MemoryStream memStreamOfFixedImage = new MemoryStream();
        fixedImage.Save(memStreamOfFixedImage, image.RawFormat);
        fixedImage = Image.FromStream(memStreamOfFixedImage);

        return fixedImage;
    }

    public static void TestPresExport()
    {
       // String path = @"C:\Aspose Data\";
        String path = @"";
        string inputPptFile = path + "infected.ppt";
        int imageIndex = 0;

        Aspose.Slides.IPPImage img = null;

        using (Presentation presentation = new Presentation(inputPptFile))
        {
            try
            {
                int i = 0;
                foreach (IPPImage tmp in presentation.Images)
                {
                    string ImageType = "";
                    ImageType = tmp.ContentType;
                    ImageType = ImageType.Remove(0, ImageType.IndexOf("/") + 1);
                    ImageFormat Format = GetImageFormat(ImageType);
                    // string target = "C:\\Windows\\Temp" + "\\temp" + imageIndex.ToString() + "." + ImageType;
                   // path = "C:\\Aspose Data\\";

                    imageIndex++;
                    string target = path + "temp" + imageIndex.ToString() + i.ToString() + "." + ImageType;
                    //Image newImage = Image.FromFile(path + "Penguins.jpg");
                    Image ModifiedImage = null;
                    try
                    {
                        System.IO.File.WriteAllBytes(target, tmp.BinaryData);
                        
                        //Modify your image here
                        ModifiedImage = Image.FromFile(target);
                        //tmp.ReplaceImage(target); // Exception here
                        tmp.ReplaceImage(ModifiedImage); // Exception here
                        i++;

                    }
                    catch (Exception ex)
                    {
                        // ====> PLEASE USE FixImage METHOD. 
                        tmp.ReplaceImage(FixImage(ModifiedImage));
                        Console.WriteLine(ex.StackTrace);

                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            presentation.Save(path + "SavedPres.ppt", SaveFormat.Ppt);
        }
    }