728x90
반응형
728x170
▶ 메모리 손실이 없는 비트맵 소스 구하기 예제
using System.IO;
using System.Windows.Media.Imaging;
BitmapSource bitmapSource = null;
using(FileStream fileStream = new FileStream("sample.jpg", FileMode.Open))
{
bitmapSource = GetBitmapSource(fileStream);
}
728x90
▶ 메모리 손실이 없는 비트맵 소스 구하기
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
#region 비트맵 소스 구하기 - GetBitmapSource(stream)
/// <summary>
/// 비트맵 소스 구하기
/// </summary>
/// <param name="stream">스트림</param>
/// <returns>비트맵 소스</returns>
public BitmapSource GetBitmapSource(Stream stream)
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = stream;
bitmapImage.EndInit();
bitmapImage.Freeze();
BitmapSource bitmapSource = new FormatConvertedBitmap(bitmapImage, PixelFormats.Pbgra32, null, 0);
WriteableBitmap writeableBitmap = new WriteableBitmap(bitmapSource);
int width = writeableBitmap.PixelWidth;
int height = writeableBitmap.PixelHeight;
int[] pixelArray = new int[width * height];
int stride = writeableBitmap.PixelWidth * (writeableBitmap.Format.BitsPerPixel / 8);
writeableBitmap.CopyPixels(pixelArray, stride, 0);
writeableBitmap.WritePixels(new Int32Rect(0, 0, width, height), pixelArray, stride, 0);
bitmapImage = null;
return writeableBitmap;
}
#endregion
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] TreeView 클래스 : ItemContainerGenerator 속성을 사용해 바인딩 데이터의 트리 노드 구하기 (0) | 2019.06.02 |
---|---|
[C#/WPF] ComponentDispatcher 클래스 : ThreadFilterMessage 정적 이벤트를 사용해 윈도우 이벤트 가로채기 (0) | 2019.06.02 |
[C#/WPF] HwndSource 클래스 : AddHook 메소드를 사용해 윈도우 이벤트 가로채기 (0) | 2019.06.02 |
[C#/WPF] Dispatcher 클래스 : UnhandledExceptionFilter/UnhandledException 이벤트를 사용해 비정상 종료 방지하기 (0) | 2019.06.02 |
[C#/WPF] SystemColors 클래스 : 시스템 색상 표시하기 (0) | 2019.06.02 |
[C#/WPF] AppDomain 클래스 : 동일 프로세스에서 멀티 WPF 애플리케이션 실행하기 (0) | 2019.05.30 |
[C#/WPF] 복수 UI 스레드 윈도우 사용하기 (0) | 2019.05.30 |
[C#/WPF] ScrollViewer 클래스 : 마우스를 사용해 스크롤하기 (0) | 2019.05.28 |
[C#/WPF] BitmapImage 클래스 : 웹에서 비트맵 이미지 구하기 (0) | 2019.05.27 |
[C#/WPF] BitmapImage 클래스 : 비트맵 이미지 구하기 (0) | 2019.05.27 |
댓글을 달아 주세요