【发布时间】:2012-01-04 15:29:34
【问题描述】:
我有一个 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的来源 - 或者您“保存”图像控件的真正含义。图像数据本身在哪里,您到底想保存什么? -
@JonSkeet 它实际上是一个条形码图像。我编辑了帖子以包含代码。我想做的是将该图像复制/导出到我网站的文件夹中。所以结果会在网站文件夹中有一个文件:(~/fa/barcodeimages/barcode1.jpeg)。
-
@PodMays:如果您在浏览器中输入 URL,它会正确呈现 jpeg 吗?如果不是,问题出在位图的构造方式上。