【问题标题】:Export ASP Image control to a folder将 ASP Image 控件导出到文件夹
【发布时间】: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 吗?如果不是,问题出在位图的构造方式上。

标签: c# asp.net image


【解决方案1】:

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

您应该将该业务逻辑远离您的 aspx 页面的 presentation 逻辑(即提供图像以响应 URL),并将 Bitmap 创建逻辑移动到“提供条形码”和“将条形码保存到磁盘”代码都可以得到它。这可能在不同的业务逻辑程序集中,或者它可能只是在同一个项目中的一个单独的类中。最主要的是你希望它在一个可重复使用的地方。

此时,您的 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);
}

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

【讨论】:

  • 您好,条码生成码其实在Barcode.aspx(Page_Load)中。我在 (Page1.aspx) 上用Image1.ImageUrl = "~/fixedasset/GenerateBarcodeImage.aspx?d=" + Label1.Text.ToUpper(); 调用它。
  • @PodMays:但这是我的观点 - 最好将它放在表示层中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-23
  • 2020-11-04
  • 2021-05-26
  • 2013-10-10
  • 2020-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多