我有一個ASP圖像控件我想保存到特定的文件夾。將ASP圖像控件導出到文件夾
Image1.ImageUrl = "~/fa/barcode.aspx?d=" + Label1.Text.ToUpper();
這基本上就是barcode.aspx做:
Bitmap oBitmap = new Bitmap(w, 100);
// then create a Graphic object for the bitmap we just created.
Graphics oGraphics = Graphics.FromImage(oBitmap);
oGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
oGraphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
// Let's create the Point and Brushes for the barcode
PointF oPoint = new PointF(2f, 2f);
SolidBrush oBrushWrite = new SolidBrush(Color.Black);
SolidBrush oBrush = new SolidBrush(Color.White);
// Now lets create the actual barcode image
// with a rectangle filled with white color
oGraphics.FillRectangle(oBrush, 0, 0, w, 100);
// We have to put prefix and sufix of an asterisk (*),
// in order to be a valid barcode
oGraphics.DrawString("*" + Code + "*", oFont, oBrushWrite, oPoint);
Response.ContentType = "image/jpeg";
oBitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
如何將其保存到一個文件夾(〜/ FA/barcodeimages/)?到目前爲止,這是我的嘗試:
WebClient webClient = new WebClient();
string remote = "http://" + Request.Url.Authority.ToString() + "/fa/barcode.aspx?d=" + Label1.Text.ToUpper();
string local = Server.MapPath("barcodeimages/" + Label1.Text.ToUpper() + ".jpeg");
webClient.DownloadFile(remote, local);
但它不起作用,我總是得到一個損壞的.jpeg文件。而且它似乎效率低下。
你沒有解釋'oBitmap'來自哪裏 - 或者你真正想要「保存」一個圖像控件。圖像數據本身在哪裏,你想要保存什麼? – 2012-01-04 07:13:44
@JonSkeet它實際上是一個條形碼圖像。我編輯帖子以包含代碼。我想要做的是複製/導出圖像到我的網站的文件夾。所以結果會在網站文件夾中有一個文件:(〜/ fa/barcodeimages/barcode1.jpeg)。 – 2012-01-04 07:19:51
@PodMays:如果您在瀏覽器中輸入網址,它是否呈現jpeg?如果不是,則問題在於位圖構造的方式。 – shahkalpesh 2012-01-04 08:12:34