상세 컨텐츠

본문 제목

이미지 원하는 사이즈로 맞추기

Web/ASP.NET

by 탑~! 2012. 5. 18. 22:16

본문

이미지를 원하는 사이즈에 맞게 비율 맞추는 부분이다

※파라미터

-> imageURL : 이미지 파일명, wantWidth : 원하는 가로 사이즈, wantHeight: 원하는 세로 사이즈

※리턴값

-> 이미지의 "width/height" string 값으로 리턴

private string SetImagesize(string imageURL, int wantWidth, int wantHeight)
{
int imgwidth, imgheight; //바뀐 이미지 크기들
float percent; //이미지 비율

//서버 맵 패스로 이미지 경로 설정
string size = null, path = Server.MapPath(imageURL);

//이미지 존재여부 확인, 이미지가 없을경우 기본 이미지를 보임
if (System.IO.File.Exists(path) == false)
path = Server.MapPath("./UserInfo/UserImageUpload/icon_ex.gif");

//해당 경로에서 이미지 파일을 비트맵으로 로딩
Bitmap Imagesize = new Bitmap(path, false);

//불러 들인 이미지의 가로, 세로 크기 저장
imgwidth = Imagesize.Width;
imgheight = Imagesize.Height;

//크기 조정
if (imgwidth > wantWidth)
{
imgwidth = wantWidth;
percent = (float)Imagesize.Width / (float)wantWidth;
imgheight = (int)(((float)Imagesize.Height) / percent);
}
else if (imgheight > wantHeight)
{
imgheight = wantHeight;
percent = (float)Imagesize.Height / (float)wantHeight;
imgwidth = (int)((float)Imagesize.Width / percent);
}

//img 태그의 width와 height로 만들어 전달
size = imgwidth.ToString() + "/" + imgheight.ToString();
return size;
}

 

* 사용 EX

protected void btn_change_Click(object sender, ImageClickEventArgs e)
{
string TempUserImg = "";
TempUserImg = SaveFileUpLoad();
if (TempUserImg != "1")
{
img_nowimg.ImageUrl = "./userinfo/UserImageUpload/" + TempUserImg;
string tempGetsize = SetImagesize(img_nowimg.ImageUrl, 150, 150); //호출

string[] tempSize = tempGetsize.Split('/'); //배열로 만들어서 사용
img_nowimg.Width = Convert.ToInt32(tempSize[0]);
img_nowimg.Height = Convert.ToInt32(tempSize[1]);
hlf_userimg.Value = TempUserImg;
}

}

 

 

출처 : http://blog.daum.net/a-bear16/46

'Web > ASP.NET' 카테고리의 다른 글

Disabled Browser cash client side for asp.net  (0) 2013.06.17
Configuring ASP Session state on SQL server 사용  (0) 2013.05.20
브라우저 구분  (0) 2012.04.16
WebMatrix 강좌  (0) 2012.02.07
브라우저 확인하기  (0) 2012.02.07

관련글 더보기