본문 바로가기
asp.net

[본문 스크랩] [Tip]Thumbnail 이미지 만들기

by 사우람 2010. 7. 12.

[devpia에서 발췌]

 

.NET에서는 Thumbnail 만들기가 참 쉽네요.
simple한 thumbnail 만들기 클래스를 만들어 봅니다.

미리 말씀드리면 제가 thumbnail 만드는 거 알려드리는데요 저도 하나 도움받고 싶은 게 있습니다.
이 글 맨 아래에 질문드리니 아시는 분께서 한번 현답을 주셨으면 합니다... 
여기에 질문을 함께 올린다는 게 좀 애매합니다만 그래도 내용상 연결이 되어서 불가피하게 올립니다.
많은 이해 바랍니다.


Thumbnail 이미지를 만들기 위해서는 다음 사항이 결정되어야 합니다.

   1) 원본 이미지 ( 즉 파일명)
  2) thumbnail 이미지 파일명 및 저장 경로
  3) thumbnail 이미지 포맷(즉, gif 냐, jpg 냐 등..)
   4) thumbnail 이미지 사이즈 (즉, 넓이와 높이)


또한 주의사항은,
   1) thumbnail 이미지 파일이 올라갈 경로에는 웹사용자의 쓰기 권한이 할당되어야 합니다.
   2) 경로는 로컬경로로 지정하여야 Bitmap.Save 함수(아래 참조)가 작동합니다.
       단 화면에 출력할때는 웹 경로(http://www..  형식)으로 바꾸어주어야 다른 컴퓨터 브라우저로도 볼수 있습니다.
   3) thumbnail 이미지 포맷과 실제적인 thumbnail 이미지 파일의 확장자가 맞아야 합니다.
       예를 들어 이미지 포맷을 gif로 하길 원한다면 실제 thumbnail 이미지 파일 확장자도 XXX.gif 로 끝나야 합니다.

대충 이정도면 간단한 thumbnail 이미지를 만들 수 있을 것입니다.


[예제]

   - 파일명:  test.aspx,  test.aspx.cs
   - 원본 이미지 파일명 :  d:/root/file/source.jpg
   - thumbnail 이미지 파일명 및 경로 : d:/root/file/thumb.gif
   - thumbnail 이미지 포맷 : gif  ( ImageFormat.Gif 프로퍼티 활용)
   - thumbnail 이미지 사이즈 : 넓이:40px, 높이 40px

--------------------------------------
[1] test.aspx
--------------------------------------
test.aspx에서 화면에 이미지를 받을 수 있는 웹서버컨트롤을 만들어 id를 다음과 같이 지정합니다.

protected System.Web.UI.WebControls.Image ImgThumbnail;  //thumbnail 이미지 출력 컨트롤
protected System.Web.UI.WebControls.Image ImgSrc; //원본 이미지 출력 컨트롤

--------------------------------------
[2] test.aspx.cs
--------------------------------------

1) Tyumbnail 이미지 파일 생성 클래스 

     아래와 같이 thumbnail 만드는 클래스를 작성합니다.

namespace localhost
{

      public class ThumbnailMaker
      {
            //**** Callback 함수 ****
            public bool ThumbnailCallback()
            {
                  return false;
            }

             //**** Thumbnail 이미지 만들기 함수 ****
             //  width : thumbnail 이미지 폭
          //  height: thumbnail 이미지 높이
          //  srcurl: 원본 이미지 경로
          //  thmburl: thumbnail 이미지 경로
          //  imgfmt: thumbnail 이미지 포맷
          public void saveThumbnailImage(int width,int height,string srcurl,string thmburl,ImageFormat imgfmt)
             {
                    //원본 이미지 파일 생성
               System.Drawing.Bitmap img = new Bitmap(srcurl);

                    //Callback 함수 생성
               System.Drawing.Image.GetThumbnailImageAbort myCallback =
                    new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);

                    //Thumbnail 이미지 생성
               System.Drawing.Image tt = img.GetThumbnailImage(width, height, myCallback, IntPtr.Zero);
    
                    //thumbnail 이미지 저장
               tt.Save(thmburl, imgfmt );
             }
     }
    
     .....

}


  2) Page_Load 함수내에 다음과 같이 작성합니다.

      private void Page_Load(object sender, System.EventArgs e)
      {
              //원본 이미지 및 thumbnail 이미지 파일명 및 경로 지정
          string srcurl = "d:/root/file/source.jpg";
              string thumbmailurl = "d:/root/file/thumb.gif";

              // thumbnail 이미지 생성
          // thumbnail 이미지 사이즈, 원본 및 thumbnail 이미지 경로, 이미지 타입 지정
          ThumbnailMaker maker = new ThumbnailMaker();
             maker.saveThumbnailImage(70,60,srcurl,thumbmailurl,ImageFormat.Gif);

             // 이미지 화면 출력
          ImgSrc.ImageUrl = "http://www.test.com/file/source.jpg";
             ImgThumbnail.ImageUrl = "http://www.test.com/file/thumb.gif";
    }


getThumbnailImage 함수에 대해서는 msdn 을 참조하세요.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdrawingimageclassgetthumbnailimagetopic.asp