views:

39

answers:

1

This is what achieved, help please:

in pageload

{
    panelmain.Controls.Add(abc);
    panelmain.Controls.Add(grid1);
    string toexport;
    toexport = RenderControl(panelmain);

    ImageFormatConverter imgc = new ImageFormatConverter();
    System.Drawing.Image convertedimage;
    convertedimage = (System.Drawing.Image) imgc.ConvertFromString(toexport);
    Response.ContentType = "image/jpg";
    Response.AppendHeader("Content-Disposition", "inline;filename=tm.jpeg");
    Response.BufferOutput = true;
    Response.Charset = "utf-8";
    Response.Write(convertedimage);
    Response.End();
    //form1.Controls.Add(abc);
}

public string RenderControl(Control ctrl)
{
    StringBuilder sb = new StringBuilder();
    StringWriter tw = new StringWriter(sb);
    HtmlTextWriter hw = new HtmlTextWriter(tw);
    ctrl.RenderControl(hw);
    Response.Write(sb);
    return sb.ToString();
}

The error is:

ImageFormatConverter cannot convert from System.String.

A: 

You may have misinterpreted the documentation of ConvertFromString here, explanation:

You are using an ImageformatConverter class which inherits and overrides TypeConverter. This means that ConvertFromString is inherited from the superclass, but because there's no way you can convert a string to an image (unless you have a vivid imagination), this method always returns null (according to documentation), or throws a NotSupportedException. This is the default behavior of the base class and an overriding class can define custom behavior, which in your case, has not been done.

To convert from a string you will first have to define what you want. I.e., is the string a path to an image? Is it a piece of text and you want to render an image from that? Is it a Base64 encoded string that contains an image? Is it a rendered HTML page or does it contain an RTF document? Once you have an answer to these questions, you can choose the correct converter or image constructor.

EDIT: because your question seems to be about rendering HTML as an image, check out this post at SO, as also mentioned by rchern above.

Abel
thanku please have a look at other question i post with great detail, and the above solution doesnot work, it say magic code goes here? but whats the code!
http://stackoverflow.com/questions/3594544/how-to-convert-response-stream-to-an-image-revised
@user287745: I have repeated there what I said here and added a bit more detail on how you can interpret the answers in the link that I posted here.
Abel