■ WEBP 파일로 변환하는 방법을 보여준다.
▶ WEBPHelper.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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// WEBP 헬퍼 /// </summary> public static class WEBPHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region WEBP BGR 인코딩하기 - WebPEncodeBGR(sourceDataHandle, width, height, stride, quality, targetDataHandle) /// <summary> /// WEBP BGR 인코딩하기 /// </summary> /// <param name="sourceDataHandle">소스 데이터 핸들</param> /// <param name="width">너비</param> /// <param name="height">높이</param> /// <param name="stride">보폭</param> /// <param name="quality">품질</param> /// <param name="targetDataHandle">타겟 데이터 핸들</param> /// <returns>타겟 데이터 바이트 길이</returns> [DllImport("libwebp", CallingConvention = CallingConvention.Cdecl)] private static extern int WebPEncodeBGR ( IntPtr sourceDataHandle, int width, int height, int stride, float quality, out IntPtr targetDataHandle ); #endregion #region WEBP BGR 인코딩하기 (무손실) - WebPEncodeLosslessBGR(sourceDataHandle, width, height, stride, targetDataHandle) /// <summary> /// WEBP BGR 인코딩하기 (무손실) /// </summary> /// <param name="sourceDataHandle">소스 데이터 핸들</param> /// <param name="width">너비</param> /// <param name="height">높이</param> /// <param name="stride">보폭</param> /// <param name="targetDataHandle">타겟 데이터 핸들</param> /// <returns>타겟 데이터 바이트 길이</returns> [DllImport("libwebp", CallingConvention = CallingConvention.Cdecl)] private static extern int WebPEncodeLosslessBGR(IntPtr sourceDataHandle, int width, int height, int stride, out IntPtr targetDataHandle); #endregion #region WEBP 해제하기 - WebPFree(targetDataHandle) /// <summary> /// WEBP 해제하기 /// </summary> /// <param name="targetDataHandle">타겟 데이터 핸들</param> /// <returns>처리 결과</returns> [DllImport("libwebp", CallingConvention = CallingConvention.Cdecl)] private static extern int WebPFree(IntPtr targetDataHandle); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 저장하기 - Save(sourceBitmap, targetFilePath, quality) /// <summary> /// 저장하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="targetFilePath">타겟 파일 경로</param> /// <param name="quality">품질</param> public static void Save(Bitmap sourceBitmap, string targetFilePath, int quality) { Convert(sourceBitmap, out byte[] targetByteArray, quality); File.WriteAllBytes(targetFilePath, targetByteArray); } #endregion #region 저장하기 (무손실) - Save(sourceBitmap, targetFilePath) /// <summary> /// 저장하기 (무손실) /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="targetFilePath">타겟 파일 경로</param> public static void Save(Bitmap sourceBitmap, string targetFilePath) { Convert(sourceBitmap, out byte[] targetByteArray); File.WriteAllBytes(targetFilePath, targetByteArray); } #endregion //////////////////////////////////////////////////////////////////////////////// Public #region 변환하기 - Convert(sourceBitmap, targetByteArray, quality) /// <summary> /// 변환하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="targetByteArray">타겟 바이트 배열</param> /// <param name="quality">품질</param> private static void Convert(Bitmap sourceBitmap, out byte[] targetByteArray, int quality) { BitmapData sourceBitmapData = sourceBitmap.LockBits ( new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb ); int targetDataByteLength = WebPEncodeBGR ( sourceBitmapData.Scan0, sourceBitmap.Width, sourceBitmap.Height, sourceBitmapData.Stride, quality, out IntPtr targetHandle ); targetByteArray = new byte[targetDataByteLength]; Marshal.Copy(targetHandle, targetByteArray, 0, targetDataByteLength); sourceBitmap.UnlockBits(sourceBitmapData); WebPFree(targetHandle); } #endregion #region 변환하기 - Convert(sourceBitmap, targetByteArray) /// <summary> /// 변환하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="targetByteArray">타겟 바이트 배열</param> private static void Convert(Bitmap sourceBitmap, out byte[] targetByteArray) { BitmapData sourceBitmapData = sourceBitmap.LockBits ( new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb ); int targetDataByteLength = WebPEncodeLosslessBGR ( sourceBitmapData.Scan0, sourceBitmap.Width, sourceBitmap.Height, sourceBitmapData.Stride, out IntPtr targetHandle ); targetByteArray = new byte[targetDataByteLength]; Marshal.Copy(targetHandle, targetByteArray, 0, targetDataByteLength); sourceBitmap.UnlockBits(sourceBitmapData); WebPFree(targetHandle); } #endregion } } |
▶ Program.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 |
using System.Drawing; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { Bitmap sourceBitmap = new Bitmap("IMAGE\\source.png"); WEBPHelper.Save(sourceBitmap, "target1.webp", 75); WEBPHelper.Save(sourceBitmap, "target2.webp"); // 무손실 } #endregion } } |
※ 32비트 버전으로 컴파일할 수 없다.