C# 将ASP图像控件导出到文件夹

C# 将ASP图像控件导出到文件夹,c#,asp.net,image,C#,Asp.net,Image,我有一个ASP图像控件,我想将其保存到特定文件夹中 Image1.ImageUrl = "~/fa/barcode.aspx?d=" + Label1.Text.ToUpper(); 这就是条形码.aspx的基本功能: Bitmap oBitmap = new Bitmap(w, 100); // then create a Graphic object for the bitmap we just created. Graphics oGraphics =

我有一个ASP图像控件,我想将其保存到特定文件夹中

Image1.ImageUrl = "~/fa/barcode.aspx?d=" + Label1.Text.ToUpper();
这就是条形码.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/barcodemages/)中?到目前为止,我尝试了以下内容:

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文件。而且它似乎效率低下。

问题似乎在于您的业务逻辑(生成条形码图像所需的代码)位于错误的位置

您应该将该业务逻辑与您的aspx页面的表示逻辑(即提供图像以响应URL)分开,并将
位图
创建逻辑移动到“提供条形码”和“将条形码保存到磁盘”代码都可以达到的位置。它可以位于不同的业务逻辑程序集中,也可以位于同一项目中的单独类中。最重要的是,你希望它在一个可重复使用的地方

此时,您的aspx代码将更改为:

Response.ContentType = "image/jpeg";
using (Bitmap bitmap = barcodeGenerator.Generate(Code))
{
    bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
}
// TODO: Validate that the text here doesn't contain dots, slashes etc
string code = Label1.Text.ToUpper();
string file = Server.MapPath("barcodeimages/" + code + ".jpeg");
using (Bitmap bitmap = barcodeGenerator.Generate(code))
{
    bitmap.Save(file, ImageFormat.Jpeg);
}
您的保存代码将更改为:

Response.ContentType = "image/jpeg";
using (Bitmap bitmap = barcodeGenerator.Generate(Code))
{
    bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
}
// TODO: Validate that the text here doesn't contain dots, slashes etc
string code = Label1.Text.ToUpper();
string file = Server.MapPath("barcodeimages/" + code + ".jpeg");
using (Bitmap bitmap = barcodeGenerator.Generate(code))
{
    bitmap.Save(file, ImageFormat.Jpeg);
}

在这里,
barcodeGenerator
理想情况下是
barcodeGenerator
类(或任何它原来是的)的依赖注入实例。如果不使用依赖项注入,可以直接创建一个新实例,每次都指定字体等-虽然不那么令人愉快,但应该可以正常工作。

您没有解释oBitmap的来源,或者您真正的意思是“保存”图像控件。图像数据本身在哪里?您到底想保存什么?@JonSkeet它实际上是一个条形码图像。我编辑了这篇文章以包含代码。我想做的是将该图像复制/导出到我网站的文件夹中。因此,结果将是网站文件夹中有一个文件:(~/fa/barcodemages/barcode1.jpeg)。@PodMays:如果在浏览器中键入URL,它会正确呈现jpeg吗?如果不是,则问题在于位图的构造方式。您好,条形码生成代码实际上位于barcode.aspx(Page_Load)中。我用
Image1.ImageUrl=“~/fixedasset/GenerateBarcodeImage.aspx?d=“+Label1.Text.ToUpper()调用它
on(Page1.aspx)。@PodMays:但这就是我的观点——最好不要将它放在表示层中。