Quantcast
Channel: HTML Renderer
Viewing all articles
Browse latest Browse all 693

New Post: A generic error occurred in GDI+.

$
0
0
Hi,

Platform ASP.NET, C#.

System.Runtime.InteropServices.ExternalException was caught
HResult=-2147467259
Message=A generic error occurred in GDI+.
Source=System.Drawing
ErrorCode=-2147467259
StackTrace:
   at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
   at System.Drawing.Image.Save(String filename, ImageFormat format)
   at System.Drawing.Image.Save(String filename)
   at PreTwitters.helper.Twitter.Tweet(String authorid, String poemJSON) in C:\Projects\Twitter\PreTwitters\PreTwitters\helper\Twitter.cs:line 33

InnerException:

This error is not related to the HTML Rendered. It happens when I use Image.Save to save the Image into a file.
I have checked everything the filename, directory..etc, everything is OK!

Actually, I have implemented and added this class "Converter" into the HTML Rendered in order to be able to use the "RenderToImage" function to renders the HTML into a new image of unknown size.
    public static class Converter
    {
        public static Image ToImage(string html)
        {
            Image result = HtmlRender.RenderToImage(html);
            return result;

        }
    }
And here the Coordinator class
    public class Coordinator
    {
        public static Image ToImage(string html)
        {
            Image result = TheArtOfDev.HtmlRenderer.WinForms.Converter.ToImage(html);
            return result;
        }
    }
And Finally here is the code to save the image into a file
        public static string Tweet(string authorid, string poemJSON)
        {
            
            string PhysicalPath = HttpContext.Current.Server.MapPath(".") + "\\authors\\" + authorid;
            string FileName = Poem.Base62Random() + ".jpg";
            string FilePathAndName = PhysicalPath + FileName;
            
            Poem poem = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Poem>(poemJSON);

            string html = poem.GetHTML();



            try
            {

                Image ToBeSaved = Coordinator.ToImage(html);
                ToBeSaved.Save(FilePathAndName);//, System.Drawing.Imaging.ImageFormat.Jpeg);

                /*
                I have tried the below solutions:
                1- Save it with specifing the image format
                ToBeSaved.Save(FilePathAndName, System.Drawing.Imaging.ImageFormat.Jpeg);
                2- Save it into a new image to mak sure we are able to access a free image. 
                Bitmap go = new Bitmap(ToBeSaved);
                go.Save(FilePathAndName, System.Drawing.Imaging.ImageFormat.Jpeg);
                */
            }
            catch (Exception ex)
            {
                FilePathAndName = null;
            }

            return "http://localhost:62915/authors/123/" + FileName;
        }
I appreciate your help.

Viewing all articles
Browse latest Browse all 693

Trending Articles