■ CardView 클래스의 CustomCardCaptionImage 이벤트를 사용해 카드 제목 이미지를 설정하는 방법을 보여준다.
▶ MainForm.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 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 138 139 140 141 142 |
using System; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Text; using System.IO; using DevExpress.XtraEditors; using DevExpress.XtraGrid.Views.Card; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : XtraForm { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.cardView.OptionsBehavior.AutoPopulateColumns = true; this.gridControl.DataSource = new nwindDataSetTableAdapters.EmployeesTableAdapter().GetData(); this.cardView.CustomCardCaptionImage += cardView_CustomCardCaptionImage; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 카드 뷰 카드 제목 이미지 설정하기 - cardView_CustomCardCaptionImage(sender, e) /// <summary> /// 카드 뷰 카드 제목 이미지 설정하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void cardView_CustomCardCaptionImage(object sender, CardCaptionImageEventArgs e) { DataRow dataRow = this.cardView.GetDataRow(e.RowHandle); byte[] bitmapByteArray = (byte[])dataRow["Photo"]; Bitmap bitmap = GetBitmap(bitmapByteArray); Bitmap thumbnailBitmap = GetThumbnailBitmap(bitmap, 32, 32); e.Image = thumbnailBitmap; } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 비트맵 구하기 - GetBitmap(sourceByteArray) /// <summary> /// 비트맵 구하기 /// </summary> /// <param name="sourceByteArray">소스 바이트 배열</param> /// <returns>비트맵</returns> private Bitmap GetBitmap(byte[] sourceByteArray) { MemoryStream memoryStream = new MemoryStream(sourceByteArray); Bitmap bitmap = new Bitmap(memoryStream); return bitmap; } #endregion #region 썸네일 비트맵 구하기 - GetThumbnailBitmap(sourceBitmap, targetWidth, targetHeight) /// <summary> /// 썸네일 비트맵 구하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="targetWidth">타겟 너비</param> /// <param name="targetHeight">타겟 높이</param> /// <returns>썸네일 비트맵</returns> private Bitmap GetThumbnailBitmap(Bitmap sourceBitmap, int targetWidth, int targetHeight) { Bitmap bitmap = new Bitmap(targetWidth, targetHeight); using(Graphics graphics = Graphics.FromImage(bitmap)) { graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.TextRenderingHint = TextRenderingHint.AntiAlias; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; graphics.Clear(Color.Transparent); int width; int height; if(sourceBitmap.Width < targetWidth && sourceBitmap.Height < targetHeight) { width = sourceBitmap.Width; height = sourceBitmap.Height; } else { if(sourceBitmap.Width > sourceBitmap.Height) { width = targetWidth; height = Convert.ToInt32(targetHeight * Convert.ToDouble(sourceBitmap.Height) / sourceBitmap.Width); } else { width = Convert.ToInt32(targetWidth * Convert.ToDouble(sourceBitmap.Width) / sourceBitmap.Height); height = targetHeight; } } int left = (targetWidth - width ) / 2; int top = (targetHeight - height) / 2; graphics.DrawImage(sourceBitmap, left, top, width, height); } return bitmap; } #endregion } } |