我正在尝试下载大约1000张图片。为此,我首先生成随机数,将此文本转换为图像。在按钮单击后,我正在下载此生成的图像。这工作正常。现在我想运行这个循环1000次,这样我就可以下载数千张图像。当循环运行一次时,下面的代码工作正常,但是当循环运行1000次时,它不能正常工作。
另外,我想更改应下载此图像的目标文件夹。我怎样才能做到这一点?
Below if I change value of variable i to 1000, the output is not what I am expecting
public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { for (int i = 0; i < 1; i++) { CallBUttonClick(); }
} 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; } }
Adding RandomImage.cs class file
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; } }
答案 0 :(得分:0)
您好需要在physcial文件夹中保存位图文件。我修改了你的代码。请参阅以下代码
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();
答案 1 :(得分:0)
这不是ASP.NET Page Life Cycle的工作方式。如果使用Response对象提供下载,则可以提供ONE文件。
你正在从错误的方面解决问题;你说“我想下载大约1000张图片”。如果您愿意,这是客户端的一个过程,在浏览器上。
然而,您正试图在服务器端解决此问题。
您需要1000次下载,因此您必须从客户端启动1000次下载,并让服务器端(您正在为此编写的那一页)直播1000次。
换句话说,你不能从服务器“推送下载”多个文件,你必须逐个请求它们。