【问题标题】:download generated images automatically multiple times多次自动下载生成的图像
【发布时间】:2014-01-10 07:09:35
【问题描述】:

我正在尝试下载大约 1000 张图片。首先,我生成随机数,将此文本转换为图像。单击按钮后,我正在下载此生成的图像。这工作正常。现在我想运行这个循环 1000 次,这样我就可以下载一千张图片。下面的代码在循环运行一次时工作正常,但是当循环运行 1000 次时,它并没有像我预期的那样工作。

另外,我想更改应下载此图像的目标文件夹。我怎样才能做到这一点?

如果我将变量 i 的值更改为 1000,则输出不是我所期望的

公共部分类默认值:System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { for (int i = 0; i

    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        var s = GenerateRandomCode();
        RandomImage ci = new RandomImage(s.ToString(), 300, 75);
        this.Response.Clear();
        this.Response.ContentType = "image/jpeg";
        Response.AppendHeader("Content-Disposition", "attachment; filename=downloadedFile.JPG");
        ci.Image.Save(this.Response.OutputStream, ImageFormat.Jpeg);
        ci.Dispose();

    }

    protected void CallBUttonClick()
    {
        Button1_Click(Button1, null);
    }

    private string GenerateRandomCode()
    {
        Random r = new Random();
        string s = "";
        for (int j = 0; j < 5; j++)
        {
            int i = r.Next(3);
            int ch;
            switch (i)
            {
                case 1:
                    ch = r.Next(0, 9);
                    s = s + ch.ToString();
                    break;
                case 2:
                    ch = r.Next(65, 90);
                    s = s + Convert.ToChar(ch).ToString();
                    break;
                case 3:
                    ch = r.Next(97, 122);
                    s = s + Convert.ToChar(ch).ToString();
                    break;
                default:
                    ch = r.Next(97, 122);
                    s = s + Convert.ToChar(ch).ToString();
                    break;
            }
            r.NextDouble();
            r.Next(100, 1999);
        }
        return s;
    }
}

添加 RandomImage.cs 类文件

using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Drawing.Text; using System; public class RandomImage { //Default Constructor public RandomImage() { } //property public string Text { get { return this.text; } } public Bitmap Image { get { return this.image; } } public int Width { get { return this.width; } } public int Height { get { return this.height; } } //Private variable private string text; private int width; private int height; private Bitmap image; private Random random = new Random(); //Methods declaration public RandomImage(string s, int width, int height) { this.text = s; this.SetDimensions(width, height); this.GenerateImage(); } public void Dispose() { GC.SuppressFinalize(this); this.Dispose(true); } protected virtual void Dispose(bool disposing) { if (disposing) this.image.Dispose(); } private void SetDimensions(int width, int height) { if (width <= 0) throw new ArgumentOutOfRangeException("width", width, "Argument out of range, must be greater than zero."); if (height <= 0) throw new ArgumentOutOfRangeException("height", height, "Argument out of range, must be greater than zero."); this.width = width; this.height = height; } private void GenerateImage() { Bitmap bmp = new Bitmap(1, 1); Graphics graphics = Graphics.FromImage(bmp); Font font = new Font(FontFamily.GenericSansSerif, 28); SizeF stringSize = graphics.MeasureString(this.text, font); bmp = new Bitmap(bmp, (int)stringSize.Width+30, (int)stringSize.Height+30); graphics = Graphics.FromImage(bmp); graphics.DrawString(this.text, font, Brushes.White, 0, 0); font.Dispose(); graphics.Flush(); graphics.Dispose(); this.image = bmp; } }

【问题讨论】:

    标签: asp.net image


    【解决方案1】:

    您好,您需要将位图文件保存在物理文件夹中。我已经修改了你的代码。请看下面的代码

    private void GenerateImage()
        {
            Bitmap bmp = new Bitmap(1, 1);
    
            Graphics graphics = Graphics.FromImage(bmp);
            Font font = new Font(FontFamily.GenericSansSerif, 28);
            SizeF stringSize = graphics.MeasureString(this.text, font);
            bmp = new Bitmap(bmp, (int)stringSize.Width + 30, (int)stringSize.Height + 30);
            graphics = Graphics.FromImage(bmp);
            graphics.DrawString(this.text, font, Brushes.White, 0, 0);
            font.Dispose();
            graphics.Flush();
            graphics.Dispose();
            bmp.Save("C:\\" + this.text + ".jpg");
            this.image = bmp;
    
    
        }
    

    我还从按钮单击中删除了下面的代码并休息了,它工作正常。

    //this.Response.Clear();
            //this.Response.ContentType = "image/jpeg";
            //Response.AppendHeader("Content-Disposition", "attachment; filename=downloadedFile.JPG");
    
            //ci.Image.Save(this.Response.OutputStream, ImageFormat.Jpeg);
            //ci.Dispose();
    

    【讨论】:

    • 我只添加了值 1000。那么你将不会得到我所期望的输出。您可以使用示例应用程序中的代码进行检查
    • 它基本上是添加“downloadFile.jpeg,attachment”、“downloadFile(1).jpeg,attachment”作为文件名,我不想为正在下载的 1000 个图像编辑重命名文件名。 .so 任何方式来更正为 i 大于 1 时下载的图像提供的文件名
    • 您能否建议对我的代码本身进行任何编辑以完成上述工作?
    • 是 RandomImage 第三方工具吗?
    • 不...我为生成图像而编写的类文件。
    【解决方案2】:

    ASP.NET Page Life Cycle 不是这样工作的。如果您使用 Response 对象来提供下载,则可以提供 ONE 文件。

    你从错误的角度来解决问题;你说“我正在尝试下载大约 1000 张图片”。这是客户端的一个过程,如果你愿意的话,在浏览器上。

    但您正试图在服务器端解决这个问题。

    您想要 1000 次下载,因此您必须从客户端启动 1000 次下载,并让服务器端,即您为此编写的页面,经历 1000 次。

    换句话说,您不能从服务器“推送下载”多个文件,您必须逐个请求它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-17
      • 2013-07-06
      • 1970-01-01
      • 1970-01-01
      • 2020-06-10
      • 1970-01-01
      相关资源
      最近更新 更多