【问题标题】:Capture div as image using asp.net - c#使用 asp.net - c# 将 div 捕获为图像
【发布时间】:2016-12-26 08:08:45
【问题描述】:

在我的 c# windows 应用程序中,我使用下面的代码成功地将 panel1 捕获为图像(bmp、jpg、png)

int x1 = SystemInformation.WorkingArea.X;
int y1 = SystemInformation.WorkingArea.Y;
int width1 = panel1.Width;
int height1 = panel1.Height;
Rectangle bounds1 = new Rectangle(x1, y1, width1, height1);
Bitmap img1 = new Bitmap(width1, height1);
panel1.DrawToBitmap(img1, bounds1);

我在我的 asp 网站中使用此代码将 asp 面板捕获为图像。但是SystemInformationDrawToBitmap 在网络项目中不起作用。谁能给我一个想法,如何捕获具有实际宽度和高度的 asp 面板。

我的完整 Windows 应用程序按钮代码如下

private void savetypebtn_Click(object sender, EventArgs e)
    {
        try
        {
            int x1 = SystemInformation.WorkingArea.X;
            int y1 = SystemInformation.WorkingArea.Y;
            int width1 = panel1.Width;
            int height1 = panel1.Height;
            Rectangle bounds1 = new Rectangle(x1, y1, width1, height1);
            Bitmap img1 = new Bitmap(width1, height1);
            panel1.DrawToBitmap(img1, bounds1);

            string saved_file = "";
            savedialog.InitialDirectory = "D:";
            savedialog.Title = "Save Quotation";
            savedialog.FileName = "";
            savedialog.Filter = "Jpg image|*.jpg|Bitmap image|*.bmp|png image|*.png";

            if (savedialog.ShowDialog() != DialogResult.Cancel)
            {
                saved_file = savedialog.FileName;
                img1.Save(saved_file, System.Drawing.Imaging.ImageFormat.Bmp);
            }
        }
        catch (Exception ex)
        {
            errorlbl.Visible=True;
            errorlbl.Text = ex.Message;
        }

【问题讨论】:

  • .net 4.0 后不支持DrawToBitmap

标签: c# asp.net


【解决方案1】:

这是我找到的解决方案。首先获取一个控件的渲染 html 输出,如下所示How do I get the HTML output of a UserControl in .NET (C#)?

StringBuilder myStringBuilder = new StringBuilder();
TextWriter myTextWriter = new StringWriter(myStringBuilder);
HtmlTextWriter myWriter = new HtmlTextWriter(myTextWriter);
Panel1.RenderControl(myWriter);
string html = myTextWriter.ToString();   

然后如这里所示Convert HTML string to image将html转换成图片

 public void ConvertHtmlToImage()
{

  System.Drawing.Image img = HtmlRender.RenderToImage(html, new Size(400, 200), Color.Linen);//html is the varialbe obtained above
   img.Save(Server.MapPath("/Images/save.jpg"), ImageFormat.Jpeg);
}

您必须将 HTMLrenderer 库添加到您的项目中才能正常工作

【讨论】:

  • @Narendra:这对你有用吗?如果可以,请您将其标记为答案吗?
猜你喜欢
  • 1970-01-01
  • 2013-01-13
  • 2014-04-03
  • 1970-01-01
  • 2017-12-24
  • 1970-01-01
  • 2011-04-01
  • 1970-01-01
相关资源
最近更新 更多