如何使用ImageFormatConverter.ConvertFromString包含HTML响应图像的字符串转换? [英] How can I use ImageFormatConverter.ConvertFromString to convert a string containing HTML response to an image?
问题描述
这是什么实现,帮助请:
在页面加载
{
panelmain.Controls.Add(ABC);
panelmain.Controls.Add(GRID1);
串toexport;
toexport = RenderControl(panelmain); ImageFormatConverter IMGC =新ImageFormatConverter();
为System.Drawing.Image convertedimage;
convertedimage =(System.Drawing.Image对象)imgc.ConvertFromString(toexport);
Response.ContentType =图像/ JPG
Response.AppendHeader(内容处置,内联;文件名= tm.jpeg);
Response.BufferOutput =真;
Response.Charset的=UTF-8;
的Response.Write(convertedimage);
到Response.End();
//form1.Controls.Add(abc);
}公共字符串RenderControl(控制CTRL)
{
StringBuilder的SB =新的StringBuilder();
StringWriter的TW =新的StringWriter(某人);
HtmlTextWriter的HW =新的HtmlTextWriter(TW);
ctrl.RenderControl(HW);
回复于(某人);
返回sb.ToString();
}
该错误是:
ImageFormatConverter不能从System.String转换。
块引用>解决方案您可能misinter preTED的文档
ConvertFromString
在这里,解释:您使用的是
ImageformatConverter
类,它继承并重写的TypeConverter
。这意味着ConvertFromString
从超类继承的,但因为没有办法,你可以将字符串转换为图像(除非你有一个生动的想象力),此方法始终返回null (根据文档),或抛出一个引发NotSupportedException
。这是基类的默认行为和压倒一切的类可以定义自定义的行为,而你的情况,的尚未完成。要从一个字符串,你首先必须定义你想要的转换。即,是字符串图像的路径?它是一段文字,并要渲染从一个图像?它是一个Base64编码的连接包含图像codeD字符串?它是一个呈现的HTML页面或它包含的RTF文档?一旦你有了这些问题的答案,就可以选择正确的转换器或图像构造。
编辑:因为你的问题似乎是有关呈现HTML作为一个图像,请这个职位在SO ,也是由 rchern上面提到
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.
解决方案You may have misinterpreted the documentation of
ConvertFromString
here, explanation:You are using an
ImageformatConverter
class which inherits and overridesTypeConverter
. This means thatConvertFromString
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 aNotSupportedException
. 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.
这篇关于如何使用ImageFormatConverter.ConvertFromString包含HTML响应图像的字符串转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!