2016-08-19 24 views
0

在我的C#Windows應用程序,我成功地使用下面的代碼捕獲DIV作爲圖像 - C#

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); 

拍攝PANEL1作爲圖像(BMP,JPG,PNG),我用這個代碼在我的ASP網站捕獲asp面板作爲圖像。但SystemInformationDrawToBitmap在web項目中不起作用。任何人都可以給我一個想法,如何捕捉具有實際寬度和高度的ASP面板。下面

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; 
     } 
+0

.net 4.0後不支持DrawToBitmap –

回答

0

我完整的Windows應用程序按鈕的代碼被賦予這是我找到了解決辦法。首先拿到的一個控件的渲染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庫添加到您的項目這工作

+0

@Narendra:這是否適合你?如果是這樣,你能否把這個標記爲答案? – TechTurtle