■ Bitmap 클래스를 사용해 투명 비트맵을 구하는 방법을 보여준다.
▶ 예제 코드 (C#)
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 |
using System; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; #region 투명 비트맵 구하기 - GetTransparentBitmap(sourceImage, alpha) /// <summary> /// 투명 비트맵 구하기 /// </summary> /// <param name="sourceImage">소스 이미지</param> /// <param name="alpha">알파</param> /// <returns>투명 비트맵</returns> public Bitmap GetTransparentBitmap(Image sourceImage, byte alpha = 100) { Bitmap targetBitmap = GetBitmap(sourceImage); BitmapData targetBitmapData = targetBitmap.LockBits ( new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb ); IntPtr sourceHandle = targetBitmapData.Scan0; byte[] targetByteArray = new byte[targetBitmapData.Stride * targetBitmap.Height]; Marshal.Copy(sourceHandle, targetByteArray, 0, targetByteArray.Length); for(int i = 3; i < targetByteArray.Length; i += 4) { targetByteArray[i] = alpha; } Marshal.Copy(targetByteArray, 0, sourceHandle, targetByteArray.Length); targetBitmap.UnlockBits(targetBitmapData); targetBitmapData = null; targetByteArray = null; return targetBitmap; } #endregion |