Global.asax.cs 파일에 해당 부분 추가

 

1
2
3
4
protected void Application_Start()
{
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
}
 
cs

 

'공부 > C#' 카테고리의 다른 글

C# FTP 이미지업로드  (0) 2022.05.06
유니코드 한글로 변환하기  (0) 2022.05.06
직사각형 배경에 정사각형 이미지 넣기  (0) 2022.02.24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
for (int i = startIndex; i <= page; i++)
{
    MakeDirectory("ftp://ftp.ftp주소/"); //경로 검사 후 경로 생성
    Thread.Sleep(int.Parse(TimeSpan.FromSeconds(2).TotalMilliseconds.ToString())); //2초 쉬기
    imageDt;
    Parallel.ForEach(imageDt.AsEnumerable(), row => //병렬로 처리
    {
        string extension = Path.GetExtension(url); //이미지 확장자 구하기(.jpg나 .png)
        
        string 업로드할이미지경로= string.Empty;//업로드될 ftp경로 및 이름
        string 원본이미지url= string.Empty;//업로드할 url(이미지링크)
        업로드할이미지경로= "" + extension; 
        
        result = UploadFileList(원본이미지url, 업로드할이미지경로);
        results.Add(result);
    });
}
 
// 업로드 함수
private UploadImageResult UploadFileList(string imageUrl, string ftpPath)
{
    string returnUrl = "https://ftp주소/";
    returnUrl = returnUrl + ftpPath;//업로드되고 업로드 된 ftp의 경로
    try
    {
        
        string ftpUrl = string.Empty;
        ftpUrl = "ftp://ftp.ftp주소/" + ftpPath;
 
        FtpWebRequest req = Connect(ftpUrl);
        byte[] buffer = new byte[0];
        buffer = 이미지 바이트;
        using (var ftpStream = req.GetRequestStream())
        {
            ftpStream.Write(buffer, 0, buffer.Length);
        }
 
        // 응답 표시
        using (var resp = (FtpWebResponse)req.GetResponse())
        {
            if (resp.StatusCode == FtpStatusCode.ClosingData)//성공
            {
                
            }
            else
            {
                throw new Exception("FTP 업로드 실패");
            }
        }
 
        return result;
 
    }
    catch(Exception ex)
    {
        
    }
 
    return result ;
}
 
//FTP연결 함수
private FtpWebRequest Connect(string ftpUrl)
{
    FtpWebRequest req = (FtpWebRequest)WebRequest.Create(ftpUrl);
    req.KeepAlive = true;
    req.Method = WebRequestMethods.Ftp.UploadFile;
    req.Credentials = new NetworkCredential("ftp아이디""ftp비밀번호");
    req.UsePassive = true;
    return req;
}
 
//경로검사 후 경로없으면 디렉터리 생성
private void MakeDirectory(string directory)
{
    FtpWebRequest req = Connect(directory);
 
    req.Method = WebRequestMethods.Ftp.ListDirectory;
    try
    {
        using (var result = (FtpWebResponse)req.GetResponse())
        {
            return;
        }
    }
    catch
    {
        req = Connect(directory);
        req.Method = WebRequestMethods.Ftp.MakeDirectory;
        using (var resp = (FtpWebResponse)req.GetResponse())
        {
            Console.WriteLine(resp.StatusCode);
        }
    }
}
 
cs
1
2
3
4
5
6
7
8
9
10
static string DecodeEncodedNonAsciiCharacters(string value)
{
    return Regex.Replace(
           value,
           @"\\u(?<Value>[a-zA-Z0-9]{4})",
           m =>
           {
               return ((char)int.Parse(m.Groups["Value"].Value, NumberStyles.HexNumber)).ToString();
           });
}
 
cs

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public bool ResizeImage(string url)
{
    public byte[] FileBytes;
    Scraper scraper = new Scraper();
    scraper.AllowAutoRedirect = true;
    scraper.Referer = "";
    scraper.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36";
    scraper.Go(url);
 
    Image image;
    using (var ms = new MemoryStream())
    {
        image = Image.FromStream(scraper.Stream);
        image = (Image)new Bitmap(image, new Size(960960));
        string Name = @"c:\temp\";
        string pageFilePath = Path.Combine(Name, "tes11t" + ".jpg");
        Bitmap bmp = new Bitmap(960, 1280);
        using (Graphics grahp = Graphics.FromImage(bmp))
        {
            grahp.Clear(Color.White);
            grahp.DrawImage(image, new Rectangle(0, 160, 960, 960));
            Name = @"c:\temp\";
            pageFilePath = Path.Combine(Name, "test" + ".jpg");
            bmp.Save(pageFilePath, ImageFormat.Jpeg);
            image = (Image)bmp;
            image.Save(ms, ImageFormat.Jpeg);
        }
        FileBytes = ms.ToArray();
    }
    return true;
 
}
cs

 

url : 이미지 경로

Scraper.Go(url) : 이미지 로드하기위해 크롤링

13번째줄 : 크롤링한 이미지를 스트림으로 읽어 이미지로 변환

14번째줄 : 2500 X 2500인 이미지를 960 X 960으로 변환

20번째줄 : 배경을 흰색으로 칠함

21번째줄 : 960 X 1280 인 흰색 직사각형 배경에 위에서 변환한 이미지를 삽입

0,160 좌표에부터 그리기. 이미지 사이즈는 960 X 960

24번째줄 : 컴퓨터에 해당 이미지 저장하기(확인용)

26번째줄 : 메모리스트림을 이미지로 저장

28번째줄 : 메모리스트림을 버퍼로 변환

 

'공부 > C#' 카테고리의 다른 글

.net framework에 TLS 버전 수동으로 설정해주는 방법  (0) 2022.06.10
C# FTP 이미지업로드  (0) 2022.05.06
유니코드 한글로 변환하기  (0) 2022.05.06

+ Recent posts