locked
Convert Div to Image RRS feed

  • Question

  • User-2069879592 posted

    I have a div containing gridview and table, need to convert this div to an Image which will be sent in mail and printed. What are the best possible solutions for it.

    Regards,

    Bala

    Tuesday, February 11, 2014 2:11 AM

Answers

  • User-2069879592 posted

    The best thing to do is to create html mail instead of taking image.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 19, 2014 11:51 PM

All replies

  • User1027872733 posted

    // create the HTML to Image converter
    HtmlToImage htmlToImageConverter = new HtmlToImage();

    // set browser width
    htmlToImageConverter.BrowserWidth = int.Parse(textBoxBrowserWidth.Text);

    // set HTML Load timeout
    htmlToImageConverter.HtmlLoadedTimeout = int.Parse(textBoxLoadHtmlTimeout.Text);

    // set whether the resulted image is transparent
    htmlToImageConverter.TransparentImage =
                (dropDownListImageFormat.SelectedValue == "PNG") ?
                checkBoxTransparentImage.Checked : false;

    // set triggering mode
    // for WaitTime mode set the wait time before convert
    switch (dropDownListTriggeringMode.SelectedValue)
    {
        case "Auto":
            htmlToImageConverter.TriggerMode = ConversionTriggerMode.Auto;
            break;
        case "WaitTime":
            htmlToImageConverter.TriggerMode = ConversionTriggerMode.WaitTime;
            htmlToImageConverter.WaitBeforeConvert = int.Parse(textBoxWaitTime.Text);
            break;
        case "Manual":
            htmlToImageConverter.TriggerMode = ConversionTriggerMode.Manual;
            break;
        default:
            htmlToImageConverter.TriggerMode = ConversionTriggerMode.Auto;
            break;
    }

    // convert to image
    System.Drawing.Image imageObject = null;
    string imageFormatName = dropDownListImageFormat.SelectedValue.ToLower();
    string imageFileName = String.Format("HtmlToImage.{0}", imageFormatName);

    if (radioButtonConvertUrl.Checked)
    {
        // convert URL
        string url = textBoxUrl.Text;

        imageObject = htmlToImageConverter.ConvertUrlToImage(url)[0];
    }
    else
    {
        // convert HTML code
        string htmlCode = textBoxHtmlCode.Text;
        string baseUrl = textBoxBaseUrl.Text;

        imageObject = htmlToImageConverter.ConvertHtmlToImage(htmlCode, baseUrl)[0];
    }

    // get the image buffer in the selected image format
    byte[] imageBuffer = GetImageBuffer(imageObject);

    // the image object rturned by converter can be disposed
    imageObject.Dispose();

    // inform the browser about the binary data format
    string mimeType = imageFormatName == "jpg" ? "jpeg" : imageFormatName;
    HttpContext.Current.Response.AddHeader("Content-Type", "image/" + mimeType);

    // let the browser know how to open the image
    HttpContext.Current.Response.AddHeader("Content-Disposition",
                String.Format("attachment; filename={0};
                        size={1}",
                imageFileName, imageBuffer.Length.ToString()));

    // write the image buffer to HTTP response
    HttpContext.Current.Response.BinaryWrite(imageBuffer);

    // call End() method of HTTP response
    // to stop ASP.NET page processing
    HttpContext.Current.Response.End();

    Tuesday, February 11, 2014 2:54 AM
  • User-2069879592 posted

    How to refer class HtmlToImage.

    Wednesday, February 12, 2014 7:25 AM
  • User1027872733 posted

    http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlimage(v=vs.110).aspx

    Thursday, February 13, 2014 11:50 AM
  • User-760709272 posted

    How to refer class HtmlToImage.

    I think it is part of the iTextSharp library.

    Thursday, February 13, 2014 12:07 PM
  • User1027872733 posted

    http://forums.asp.net/t/1764959.aspx

    Thursday, February 13, 2014 1:10 PM
  • User-933407369 posted

    i would suggest you check out the link for details:

    Convert HTML string to image [closed]

    Codes as shown below:

    public void ConvertHtmlToImage()
    {
       Bitmap m_Bitmap = new Bitmap(400, 600);
       PointF point = new PointF(0, 0);
       SizeF maxSize = new System.Drawing.SizeF(500, 500);
       HtmlRenderer.HtmlRender.Render(Graphics.FromImage(m_Bitmap),
                                               "<html><body><p>This is a shitty html code</p>"
                                               + "<p>This is another html line</p></body>",
                                                point, maxSize);
    
       m_Bitmap.Save(@"C:\Test.png", ImageFormat.Png);
    }

    Monday, February 17, 2014 6:52 AM
  • User-2069879592 posted

    The best thing to do is to create html mail instead of taking image.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 19, 2014 11:51 PM