728x90
반응형
728x170
■ BitmapImage 클래스를 사용해 WINFORM Bitmap 객체에서 비트맵 이미지를 구하는 방법을 보여준다.
▶ MainWindow.xaml
<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
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
}
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF/.NET6] Screen 클래스 : FromHandle 정적 메소드를 사용해 Window 객체의 화면 구하기 (0) | 2022.10.02 |
---|---|
[C#/WPF/.NET6] WPF 프로젝트에서 WinForm 사용하기 (0) | 2022.10.02 |
[C#/WPF/.NET6] 누겟 설치 : Microsoft.Web.WebView2 (0) | 2022.09.30 |
[C#/WPF/.NET6] WebView2 엘리먼트 : WebView2 미설치시 자동 설치하기 (0) | 2022.09.30 |
[C#/WPF] Canvas 클래스 : 캔버스 확장 사용하기 (0) | 2022.08.25 |
[C#/WPF/.NET6] Image 클래스 : 움직이는 GIF 이미지 만들기 (0) | 2022.08.20 |
[C#/WPF] BlurEffect 엘리먼트 : 네온싸인 효과 만들기 (0) | 2022.07.23 |
[C#/WPF] DropShadowEffect 엘리먼트 : 네온싸인 효과 만들기 (0) | 2022.07.23 |
[C#/WPF] DropShadowEffect 엘리먼트 : 네온싸인 효과 만들기 (0) | 2022.07.23 |
[C#/WPF] ControlTemplate 엘리먼트 : ToggleButton 엘리먼트를 정의해 전원 버튼 만들기 (0) | 2022.07.23 |
댓글을 달아 주세요