약 1000 개의 이미지를 다운로드하려고합니다. 처음에는 난수를 생성하고이 텍스트를 이미지로 변환합니다. 이 단추를 클릭 한 후에 생성 된 이미지를 다운로드합니다. 이것은 잘 작동합니다. 이제 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; } }
추가 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; } }
값 1000 만 추가했습니다. 그렇다면 내가 기대하는 결과를 얻지 못할 것입니다. 샘플 응용 프로그램의 코드를 사용하여 – Aakansha
을 확인할 수 있습니다. 기본적으로 파일 이름으로 "downloadFile.jpeg, attachment", "downloadFile (1) .jpeg, attachment"를 추가하면 1000 개의 iamges에 대한 파일명 변경 이름을 편집하려고합니다. 다운로드 한 ... 그래서 내가 1보다 큰 경우 다운로드되는 이미지에 주어진 파일 이름을 수정하는 방법 – Aakansha
당신이 참조하시기 바랍니다 : http://htmlrenderer.codeplex.com/ –