■ Image 객체에서 이미지의 RTF를 구하는 방법을 보여준다.
▶ 예제 코드 (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 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 |
using System.Drawing.Imaging; using System.Runtime.InteropServices; using System.Text; #region 이미지 RTF 구하기 - GetImageRTF(image) /// <summary> /// 이미지 RTF 구하기 /// </summary> /// <param name="image">이미지</param> /// <returns>이미지 RTF</returns> public string GetImageRTF(Image image) { const int MM_ISOTROPIC = 7; // 메타 파일이 1:1 종횡비를 유지하는지 확인한다. const int MM_ANISOTROPIC = 8; // 메타 파일의 X 좌표와 Y 좌표를 독립적으로 조정할 수 있습니다. int mappingMode = MM_ANISOTROPIC; MemoryStream stream = null; Graphics graphics = null; Metafile metaFile = null; IntPtr deviceContextHandle; try { StringBuilder stringBuilder = new StringBuilder(); stream = new MemoryStream(); using(graphics = Graphics.FromHwnd(new IntPtr(0))) { deviceContextHandle = graphics.GetHdc(); metaFile = new Metafile(stream, deviceContextHandle); graphics.ReleaseHdc(deviceContextHandle); } using(graphics = Graphics.FromImage(metaFile)) { graphics.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height)); } IntPtr emfHandle = metaFile.GetHenhmetafile(); uint bufferSize = GdipEmfToWmfBits(emfHandle, 0, null, mappingMode, EMFToWMFBitsFlag.Default); byte[] bufferByteArray = new byte[bufferSize]; GdipEmfToWmfBits(emfHandle, bufferSize, bufferByteArray, mappingMode, EMFToWMFBitsFlag.Default); for(int i = 0; i < bufferByteArray.Length; ++i) { stringBuilder.Append(string.Format("{0:X2}", bufferByteArray[i])); } return stringBuilder.ToString(); } finally { if(graphics != null) { graphics.Dispose(); } if(metaFile != null) { metaFile.Dispose(); } if(stream != null) { stream.Close(); } } } #endregion /// <summary> /// EMF→WMF 비트 플래그 /// </summary> public enum EMFToWMFBitsFlag { /// <summary> /// Default /// </summary> Default = 0x00000000, /// <summary> /// Embed EMF /// </summary> EmbedEMF = 0x00000001, /// <summary> /// Include Placeable /// </summary> IncludePlaceable = 0x00000002, /// <summary> /// No XOR Clip /// </summary> NoXORClip = 0x00000004 }; #region GDI+ EMF→WMF 변환하기 - GdipEmfToWmfBits(emfHandle, bufferSize, bufferByteArray, mappingMode, flag) /// <summary> /// GDI+ EMF→WMF 변환하기 /// </summary> /// <param name="emfHandle">EMF 핸들</param> /// <param name="bufferSize">버퍼 크기</param> /// <param name="bufferByteArray">버퍼 바이트 배열</param> /// <param name="mappingMode">매핑 모드</param> /// <param name="flag">플래그</param> /// <returns>처리 결과</returns> [DllImport("gdiplus")] private static extern uint GdipEmfToWmfBits(IntPtr emfHandle, uint bufferSize, byte[] bufferByteArray, int mappingMode, EMFToWMFBitsFlag flag); #endregion |