我正在C#中的一個項目工作,我保存我的面板有問題。 我有一個列表框,我選擇了我想要加載到我的面板中的表單。 但是,當我想保存我的面板時,無法讀取它的內容。 保存我的面板後,我想將其轉換爲PDF文檔,但是當我無法保存它時,不可能做到這一點。有人可以幫我嗎?如何在c#中保存面板的內容?
protected void btnPDF_Click(object sender, EventArgs e)
{
int teller = 0;
foreach (Control Ctrl in pnlMain.Controls)
{
if (Ctrl is Label)
{
teller++;
}
}
int teller2 = 0;
Label[] arr_label = new Label[teller];
foreach (Control Ctrl in pnlMain.Controls)
{
if (Ctrl is Label)
{
Label lbl_Ctrl = Ctrl as Label;
arr_label[teller2] = lbl_Ctrl;
teller2++;
}
}
int teller3 = 0;
int lengte = arr_label.Length;
String hulp1;
//aanmaken van uw document
var doc1 = new Document(PageSize.A4, 50, 50, 25, 25);
//aanmaken van de outputstream
var output = new MemoryStream(); //geen extra argumenten nodig
//aanmaken van een "pdfwriter document"
var writer = PdfWriter.GetInstance(doc1, output);
//voor je in je document iets kan "schrijven" moet je het eerst openen:
doc1.Open();
while (teller3 < lengte)
{
Label hulp;
hulp = arr_label[teller3];
hulp1 = hulp.Text;
teller3++;
//bepaal de inhoud van je pdf document (locatie)
string inhoud = File.ReadAllText(Server.MapPath("pdf.aspx"));
var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(hulp1), null);
//alle elementen van uw pagina in de pdf steken (foreach.... zeer handige functie)
foreach (var htmlElement in parsedHtmlElements)
doc1.Add(htmlElement as IElement);
}
//nu de pdf gemaakt is gaan we de boel afsluiten:
doc1.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename= Evaluatierapport.pdf"));
Response.BinaryWrite(output.ToArray());
}
private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.DefaultExt = "bmp";
saveFileDialog.Filter = "Bitmap files|*.bmp";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
int width = pnlMain.Width;
int height = panel.Height;
Bitmap bitMap = new Bitmap(width, height);
System.Drawing.Rectangle rec = new Rectangle(0, 0, width, height);
panel.DrawToBitmap(bitMap, rec);
bitMap.Save(saveFileDialog.FileName);
}
}
}
不可能做到這一點。怎麼會這樣?什麼錯誤?例外? – Oded 2011-05-04 13:26:14
不要看btnSaveClick下的代碼,因爲這是完全錯誤的。它在出納員(計數)上給出錯誤,他無法讀取.cs文件的內容,但是他可以在.aspx – 2011-05-04 13:33:45