■ BitmapImage 클래스를 사용해 WINFORM Bitmap 객체에서 비트맵 이미지를 구하는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <Image Name="image" Margin="10" Stretch="UniformToFill" /> </Window> |
▶ MainWindow.xaml.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 |
using System; using System.IO; using System.Windows; using System.Windows.Media.Imaging; namespace TestProject; /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); System.Drawing.Image sourceImage = System.Drawing.Image.FromFile("IMAGE/sample.png"); BitmapSource bitmapSource = GetBitmapSource(sourceImage); this.image.Source = bitmapSource; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 비트맵 소스 구하기 - GetBitmapSource(sourceImage) /// <summary> /// 비트맵 소스 구하기 /// </summary> /// <param name="sourceImage">소스 이미지</param> /// <returns>비트맵 소스</returns> private BitmapSource GetBitmapSource(System.Drawing.Image sourceImage) { MemoryStream stream = new MemoryStream(); BitmapSource bitmapSource = null; try { string guid = sourceImage.RawFormat.Guid.ToString("B").ToUpper(); switch(guid) { case "{B96B3CAA-0728-11D3-9D7B-0000F81EF32E}" : // MemoryBMP case "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}" : // BMP sourceImage.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp); stream.Position = 0; BmpBitmapDecoder bmpBitmapDecoder = new BmpBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.Default); bitmapSource = bmpBitmapDecoder.Frames[0]; break; case "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}": // PNG sourceImage.Save(stream, System.Drawing.Imaging.ImageFormat.Png); stream.Position = 0; PngBitmapDecoder pngBitmapDecoder = new PngBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.Default); bitmapSource = pngBitmapDecoder.Frames[0]; break; case "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}" : // JPEG sourceImage.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); JpegBitmapDecoder jpegBitmapDecoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.Default); stream.Position = 0; bitmapSource = jpegBitmapDecoder.Frames[0]; break; case "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}" : // GIF sourceImage.Save(stream, System.Drawing.Imaging.ImageFormat.Gif); GifBitmapDecoder gifBitmapDecoder = new GifBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.Default); stream.Position = 0; bitmapSource = gifBitmapDecoder.Frames[0]; break; case "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}" : // TIFF sourceImage.Save(stream, System.Drawing.Imaging.ImageFormat.Tiff); TiffBitmapDecoder tiffBitmapDecoder = new TiffBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.Default); stream.Position = 0; bitmapSource = tiffBitmapDecoder.Frames[0]; break; case "{B96B3CB5-0728-11D3-9D7B-0000F81EF32E}" : // ICON sourceImage.Save(stream, System.Drawing.Imaging.ImageFormat.Icon); IconBitmapDecoder iconBitmapDecoder = new IconBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.Default); stream.Position = 0; bitmapSource = iconBitmapDecoder.Frames[0]; break; case "{B96B3CAC-0728-11D3-9D7B-0000F81EF32E}" : // EMF case "{B96B3CAD-0728-11D3-9D7B-0000F81EF32E}" : // WMF case "{B96B3CB2-0728-11D3-9D7B-0000F81EF32E}" : // EXIF case "{B96B3CB3-0728-11D3-9D7B-0000F81EF32E}" : // PhotoCD case "{B96B3CB4-0728-11D3-9D7B-0000F81EF32E}" : // FlashPIX default : throw new InvalidOperationException("Unsupported image format."); } } catch { } return bitmapSource; } #endregion } |