stream - c# DeflateSream return 0; -


i'm trying compress image using defltestream somehow when try print return buffer length it's 0.

public void goo() {     memorystream ms = new memorystream();     memorystream ms2 = new memorystream();     bitmap prev;      prev = getdesktopimage();     prev.save(ms,imageformat.png);     prev.save(ms2, imageformat.png);     com = ms.toarray();     messagebox.show((com.length / 1000).tostring() + "kb");     messagebox.show(((compress(ms2).length/1000).tostring()+"kb")); } 

getdesktopimage function returns screenshot. in first messagebox display length (for example output is: "350kb"), in second 0kb!

this compress method:

private static byte[] compress(stream input) {     using (var compressstream = new memorystream())         using (var compressor = new deflatestream(compressstream, compressionmode.compress))         {             input.copyto(compressor);             compressor.close();             return compressstream.toarray();         } } 

does has idea why i'm getting 0?

when write stream (or save in case), internal position incremented accordingly.

so lasse v. karlsen said, should rewinding stream

m.position = 0; 

or

m.seek(0, seekorigin.begin) 

to make readers start read position 0.

bitmap b = new bitmap(100, 100); var m = new memorystream(); b.save(m, imageformat.png); console.writeline(m.position); // output 169 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -