사진 이미지 관련 클래스
C#2016. 4. 14. 16:15
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | /// <summary> /// 사진 정보 /// </summary> public class PhotoInfo { private int maxWidth = 200, maxHeight = 267; private int orgWidth = 0, orgHeight = 0; private int width = 0, height = 0; private bool portraitMode = false ; private string base64String = null ; /// <summary> /// 사진 정보 /// </summary> public PhotoInfo() { } /// <summary> /// 사진 정보 /// </summary> /// <param name="ms"> public PhotoInfo(MemoryStream ms) { this .base64String = "data:image/jpg;base64," + Convert.ToBase64String(ms.ToArray()); System.Drawing.Image img = System.Drawing.Image.FromStream(ms); this .orgWidth = img.Width; this .orgHeight = img.Height; this .portraitMode = (img.Height - img.Width) > 0; //세로가 긴 사진인 경우 if ( this .portraitMode) { if ( this .orgHeight > this .maxHeight) { this .height = this .maxHeight; var ratio = ( this .maxHeight * 1.0) / ( this .orgHeight * 1.0); this .width = ( int )Math.Round( this .orgWidth * ratio); if ( this .width > maxWidth) { this .width = maxWidth; ratio = ( this .maxWidth * 1.0) / ( this .orgWidth * 1.0); this .height = ( int )Math.Round( this .orgHeight * ratio); } } else { this .height = this .orgHeight; this .width = this .orgWidth; if ( this .width > maxWidth) { this .width = maxWidth; var ratio = ( this .maxWidth * 1.0) / ( this .orgWidth * 1.0); this .height = ( int )Math.Round( this .orgHeight * ratio); } } } //가로가 긴 사진인 경우(LandscapeMode) else { if ( this .orgWidth > this .maxWidth) { this .width = this .maxWidth; var ratio = ( this .maxWidth * 1.0) / ( this .orgWidth * 1.0); this .height = ( int )Math.Round( this .orgHeight * ratio); if ( this .height > maxHeight) { this .height = maxHeight; ratio = ( this .maxHeight * 1.0) / ( this .orgHeight * 1.0); this .width = ( int )Math.Round( this .orgWidth * ratio); } } else { this .width = this .orgWidth; this .height = this .orgHeight; if ( this .height > maxHeight) { this .height = maxHeight; var ratio = ( this .maxHeight * 1.0) / ( this .orgHeight * 1.0); this .width = ( int )Math.Round( this .orgWidth * ratio); } } } } /// <summary> /// 사진 최대 너비 /// </summary> public int MaxWidth { get { return maxWidth; } } /// <summary> /// 사진 최대 높이 /// </summary> public int MaxHeight { get { return maxHeight; } } /// <summary> /// 원본 사진 너비 /// </summary> public int OrgWidth { get { return this .orgWidth; } } /// <summary> /// 원본 사진 높이 /// </summary> public int OrgHeight { get { return this .orgHeight; } } /// <summary> /// 사진 너비 /// </summary> public int Width { get { return this .width; } } /// <summary> /// 사진 높이 /// </summary> public int Height { get { return this .height; } } /// <summary> /// 사진 가로방향여부 /// </summary> public bool PortraitMode { get { return this .portraitMode; } } /// <summary> /// Base64 문자열 /// </summary> public string Base64String { get { return base64String; } } /// <summary> /// 사진 정보 표시 /// </summary> /// <param name="image"> public void SetImage(Image image) { if ( this .Base64String != null ) { image.ImageUrl = this .Base64String; image.Height = this .Height; image.Width = this .width; } } } |
'C#' 카테고리의 다른 글
ClosedXML 사용 (0) | 2017.05.17 |
---|---|
ASP.NET 엑셀 출력 스타일 (0) | 2016.08.31 |
오라클 BLOB 등록 및 조회 (0) | 2016.04.14 |
다른 프로젝트의 Resources.resx 파일의 리소스에 접근하기 (0) | 2012.05.07 |
윈도우에 등록된 확장자를 가진 파일 열기 (0) | 2011.11.16 |