c# - bitmap not saving image to memory stream -
need little here. have app creating mergies 2 pictures together. issue when trying convert bitmap bitmpaimage display result on screen. can tell, image not being save memory stream @ "nwimg.save(memory,imageformat.jpeg);" ideas??
//the code //bitmap bitmapimage conversion using (memorystream memory = new memorystream()) {//nwimg type bitmap, , @ point checked properties , values did copy on merging nwimg.save(memory, imageformat.jpeg);//here image nwimg.save suppose transfer memory memory.position = 0; nwbi.streamsource = memory;//memory stream showing null nwbi.cacheoption = bitmapcacheoption.onload; }
i don't know if matter nwimg represents bitmap created merging png image on top of jpeg. didn't read said matter figured through in there.
/// here code requested david //main c#
using system; using system.collections.generic; using system.linq; using system.text; using system.windows; using system.windows.controls; using system.windows.data; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.imaging; using system.windows.navigation; using system.windows.shapes; using system.drawing; using system.io; using system.drawing.imaging; namespace picmerger2 { /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window { pic currentpic = new pic(); bitmapimage bi = new bitmapimage(new uri("\\original photo.jpg")); bitmapimage nwbi = new bitmapimage(); public mainwindow() { initializecomponent(); originalphoto.source = bi; resultphoto.source = nwbi; } private void apply_click(object sender, routedeventargs e) { bitmap nwimg; //bitmapimage bitmap conversion using (memorystream outstream = new memorystream()) { bitmapencoder enc = new bmpbitmapencoder(); enc.frames.add(bitmapframe.create(bi)); enc.save(outstream); system.drawing.bitmap markthispic = new system.drawing.bitmap(outstream); // return bitmap; <-- leads problems, stream closed/closing ... nwimg = new bitmap(markthispic); } nwimg = currentpic.mergerthesetwo(nwimg); //bitmap bitmapimage conversion using (memorystream memory = new memorystream()) { nwimg.save(memory, imageformat.jpeg); memory.position = 0; nwbi.streamsource = memory; nwbi.cacheoption = bitmapcacheoption.onload; } resultphoto.source = nwbi; } } }
//main xaml
<window x:class="picmerger2.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525"> <grid horizontalalignment="center"> <stackpanel orientation="horizontal"> <stackpanel> <image x:name="originalphoto" height="200" stretch="uniformtofill" source="{binding}"></image> <label>original images</label> </stackpanel> <button x:name="apply" click="apply_click" height="25" >apply watermark</button> <stackpanel> <image x:name="resultphoto" height="200" stretch="uniformtofill" source="{binding}"></image> <label>watermarked image</label> </stackpanel> </stackpanel> </grid> </window>
// pic class
using system; using system.collections.generic; using system.linq; using system.text; using system.drawing; using system.drawing.drawing2d; namespace picmerger2 { class pic { bitmap watermark = new bitmap(picmerger2.properties.resources._watermark); public bitmap mergerthesetwo(bitmap bottomimage) { try { using (var canvas = graphics.fromimage(bottomimage)) { canvas.interpolationmode = interpolationmode.highqualitybicubic; // canvas.drawimage(bottomimage, new rectangle(0, 0, bottomimage.width, bottomimage.height), new rectangle(0, 0, bottomimage.width, bottomimage.height), graphicsunit.pixel); canvas.drawimage(watermark, 0, 0); canvas.save(); //save current picture bitmap newimage = new bitmap(bottomimage.width, bottomimage.height, canvas); return newimage; } } catch (exception) { throw; } } } }
you need change couple of things code can work.
use following code bitmap bitmapimage conversion.
using (memorystream memory = new memorystream()) { nwimg.save(memory, imageformat.jpeg); memory.position = 0; nwbi = new bitmapimage(); nwbi.begininit(); nwbi.streamsource = memory; nwbi.cacheoption = bitmapcacheoption.onload; nwbi.endinit(); }
inside pic class, replace these lines
//save current picture bitmap newimage = new bitmap(bottomimage.width, bottomimage.height, canvas); return newimage;
to this
return bottomimage;
since overload of bitmap class using, doesn't create new bitmap based on graphics object resolution (this results empty image). so, since draw onto bottomimage bitmap, need return image.
Comments
Post a Comment