c# - How to compress image byte[] array to JPEG/PNG and return ImageSource object -
i have image (in form of byte[] array) , want compressed version of it. either png or jpeg compressed version.
i use following code @ minute:
private media.imagesource getimage(byte[] imagedata, system.windows.media.pixelformat format, int width = 640, int height = 480) { return system.windows.media.imaging.bitmapsource.create(width, height, 96, 96, format, null, imagedata, width * format.bitsperpixel / 8); }
how extend can compress , return compressed version of image source (with degraded quality).
thanks in advance!
using right encoder pngbitmapencoder should work:
private imagesource getimage(byte[] imagedata, system.windows.media.pixelformat format, int width = 640, int height = 480) { using (memorystream memorystream = new memorystream()) { pngbitmapencoder encoder = new pngbitmapencoder(); encoder.interlace = pnginterlaceoption.on; encoder.frames.add(bitmapframe.create(bitmapsource.create(width, height, 96, 96, format, null, imagedata, width * format.bitsperpixel / 8))); encoder.save(memorystream); bitmapimage imagesource = new bitmapimage(); imagesource.begininit(); imagesource.streamsource = memorystream; imagesource.endinit(); return imagesource; } }
Comments
Post a Comment