2014-01-10 2 views
0

약 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; } }

답변

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(); 
+0

값 1000 만 추가했습니다. 그렇다면 내가 기대하는 결과를 얻지 못할 것입니다. 샘플 응용 프로그램의 코드를 사용하여 – Aakansha

+0

을 확인할 수 있습니다. 기본적으로 파일 이름으로 "downloadFile.jpeg, attachment", "downloadFile (1) .jpeg, attachment"를 추가하면 1000 개의 iamges에 대한 파일명 변경 이름을 편집하려고합니다. 다운로드 한 ... 그래서 내가 1보다 큰 경우 다운로드되는 이미지에 주어진 파일 이름을 수정하는 방법 – Aakansha

+0

당신이 참조하시기 바랍니다 : http://htmlrenderer.codeplex.com/ –

0

이것은 ASP.NET Page Life Cycle의 작동 방식이 아닙니다. 응답 객체를 사용하여 다운로드를 제공하는 경우 하나의 파일을 제공 할 수 있습니다.

잘못된 쪽에서 문제가 발생하고 있습니다. 당신은 "약 1000 개의 이미지를 다운로드하려고합니다."라고 말합니다. 그것은 브라우저에서 클라이언트 측의 프로세스입니다.

그러나 서버 측에서이 문제를 해결하려고합니다.

1000 다운로드를 원하므로 클라이언트에서 1000 다운로드를 시작해야하며 서버 측에서 작성중인 한 페이지가 그 1000 회까지 살아야합니다.

즉, 서버에서 여러 파일을 "다운로드"할 수 없으므로 하나씩 요청해야합니다.

관련 문제