WPF & C# - Image 형변환 모음 ( Bitmap / BitmapImage / MemoryStream / FrameworkElement / Controls.Image / Byte[] )

2018. 8. 3. 15:11Programing (프로그래밍)/WPF & C# (C Sharp)

728x90
반응형


 WPF & C# - Image 형변환 모음 ( Bitmap / BitmapImage / MemoryStream / FrameworkElement /  Controls.Image / Byte[] )



Bitmap > MemoryStream > BitmapImage


using System.Drawing;

1
2
3
4
5
6
7
8
9
10
11
12
Bitmap bitmap = new Bitmap(this.Width, this.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
 
// Bitmap 담을 메모리스트림 준비
MemoryStream ms = new MemoryStream();   // 초기화
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);   // 
 
// BitmapImage 로 변환
var bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = ms;
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.EndInit();
cs
(확인완료)



BitmapImage > Byte | Bitmap2Byte | BitmapToByte



1
2
3
4
5
6
7
8
9
10
11
12
13
14
public Byte[] ImageToByte(BitmapImage imageSource)
{
    Stream stream = imageSource.StreamSource;
    Byte[] buffer = null;
    if (stream != null && stream.Length > 0)
    {
        using (BinaryReader br = new BinaryReader(stream))
        {
            buffer = br.ReadBytes((Int32)stream.Length);
        }
    }
 
    return buffer;
}
cs


1
2
3
4
5
6
7
byte[] data = null;
 
using(MemoryStream ms = new MemoryStream())
{
    bitmapImage.Save(ms);
    data = ms.ToArray();
}
cs



Byte[] > BitmapImage | Byte2Bitmap | ByteToBitmap



1
2
3
4
5
6
7
8
9
10
11
byte[] buffer = ...;
 
var width = 100// for example
var height = 100// for example
var dpiX = 96d;
var dpiY = 96d;
var pixelFormat = PixelFormats.Pbgra32; // for example
var bytesPerPixel = (pixelFormat.BitsPerPixel + 7/ 8;
var stride = bytesPerPixel * width;
 
var bitmap = BitmapSource.Create(width, height, dpiX, dpiY, pixelFormat, null, buffer, stride);
cs

https://stackoverflow.com/questions/15270844/how-can-i-convert-byte-to-bitmapimage



Byte[] > Controls.Image | Byte2Image | ByteToImage



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private static BitmapImage Byte2ControlsImage(byte[] imageData)
{
    if (imageData == null || imageData.Length == 0return null;
    var image = new BitmapImage();
    using (var mem = new MemoryStream(imageData))
    {
        mem.Position = 0;
        image.BeginInit();
        image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
        image.CacheOption = BitmapCacheOption.OnLoad;
        image.UriSource = null;
        image.StreamSource = mem;
        image.EndInit();
    }
    image.Freeze();
    return image;
}
cs
(확인완료)

https://stackoverflow.com/questions/9564174/convert-byte-array-to-image-in-wpf




FrameworkElement > BitmapImage



1
2
3
4
5
6
7
8
9
10
11
12
private static RenderTargetBitmap Element2BitmapImage(FrameworkElement element)
{
    DrawingVisual drawingVisual = new DrawingVisual();
    DrawingContext drawingContext = drawingVisual.RenderOpen();
    drawingContext.DrawRectangle(new VisualBrush(element), nullnew Rect(new Point(00), new Point(element.ActualWidth, element.ActualHeight)));
    drawingContext.Close();
 
    RenderTargetBitmap target = new RenderTargetBitmap((int)element.ActualWidth, (int)element.ActualHeight, 9696System.Windows.Media.PixelFormats.Pbgra32);
 
    target.Render(drawingVisual);
    return target;
}
cs




Uri > byte[] | Uri2Byte | UriToByte



1
2
3
4
5
6
7
8
byte[] imageBytes = null;
using (MemoryStream ms = new MemoryStream())
{
    System.Windows.Media.Imaging.BmpBitmapEncoder bbe = new BmpBitmapEncoder();
    bbe.Frames.Add(BitmapFrame.Create(new Uri(img.Source.ToString(), UriKind.RelativeOrAbsolute)));
    bbe.Save(ms);
    imageBytes = ms.ToArray();
}
cs







728x90
반응형