첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

■ BitmapImage 클래스를 사용해 WINFORM Bitmap 객체에서 비트맵 이미지를 구하는 방법을 보여준다.

TestProject.zip
3.65MB

▶ 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
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요