c# - How to read image inside (as a part of) a stream? -
i have files structure
+-------------+-------------+---------------+---------+-------------+ | img1_offset | img1_length | custom info | image 1 | image 2 | +-------------+-------------+---------------+---------+-------------+
now want read image 1
image control. 1 possible way open file in stream (filestream
), copy image 1 part other stream (i1_stream
) read image i1_stream
. code i'm using:
using (filestream filestream = new filestream(path, filemode.open, fileaccess.read)) { using (memorystream i1_stream = new memorystream()) { filestream.seek(500, seekorigin.begin); // i1_offset filestream.copyto(i1_stream, 30000); // i1_length var bitmap = new bitmapimage(); bitmap.begininit(); bitmap.cacheoption = bitmapcacheoption.onload; bitmap.streamsource = i1_stream; bitmap.endinit(); return bitmap; } }
because need open many file @ 1 time (ie. load 50 images 50 files wrappanel), think better if can read image 1
directly filestream
. how can that? thank!
first, should read array of image bytes input stream. copy new bitmap:
var imagewidth = 640; // read value image metadata stream part var imageheight = 480 // same width var bytes = stream.read(..) // array length must width * height using (var image = new bitmap(imagewidth, imageheight)) { var bitmapdata = image.lockbits(new rectangle(0, 0, imagewidth, imageheight), system.drawing.imaging.imagelockmode.readwrite, // r/w memory access image.pixelformat); // possibly should read stream // copying system.runtime.interopservices.marshal.copy(bytes, 0, bitmapdata.scan0, bitmapdata.height * bitmapdata.stride); image.unlockbits(bitmapdata); // work bitmap }
Comments
Post a Comment