Image To Png Stream - CSharp System.Drawing

CSharp examples for System.Drawing:PNG

Description

Image To Png Stream

Demo Code


using System.Windows.Media.Imaging;
using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.IO;/*from   w w  w.j  a v a2s . co m*/
using System.Drawing.Imaging;
using System.Drawing;
using System.Collections.Generic;
using System;

public class Main{
        public static Stream ToPngStream(this System.Windows.Controls.Image image)
        {
            if (image == null || image.Source == null)
                return null;

            MemoryStream memStream = new MemoryStream();
            PngBitmapEncoder encoder = new PngBitmapEncoder();

            var frame = encoder.With( x => image.Source as CroppedBitmap)
                .With( x => BitmapFrame.Create(x));
            if (frame == null)
                return null;

            encoder.Frames.Add(frame);
            encoder.Save(memStream);
            return memStream;
        }
}

Related Tutorials