Best way to save snapshots
Hi,
Since camera frames are in memory there are no direct way of saving images in jpg files.
I'm doing it using WriteableBitmap and JPegEncoder. I don't know of there is a better or efficient way.
Thanks for your help.
var frameDimensions = new FrameDimensions(frame.Width, frame.Height, frame.Stride);
var frameData = new byte[frameDimensions.Stride * frameDimensions.Height];
frame.CopyTo(frameData);
var writeableBmp = new WriteableBitmap(frame.Dimensions.Width, frame.Dimensions.Height, 96.0, 96.0, PixelFormats.Rgb24, null);
var rect = new Int32Rect(0, 0, frame.Dimensions.Width, frame.Dimensions.Height);
writeableBmp.WritePixels(rect, frame.FrameData, frame.Dimensions.Stride, 0);
using (FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))
{
var encoder = new JpegBitmapEncoder();
encoder.Rotation = rotation.GetMediaImagingRotation();
encoder.Frames.Add(BitmapFrame.Create(writeableBmp));
encoder.Save(fileStream);
}
-
Hi Juan Diezyanguas I note that you asked about writing to a jpg image with C# in the past case at the link below.
https://support.intelrealsense.com/hc/en-us/community/posts/21724705791251-Save-Raw-DepthFrame-in-c
In regard to your current question in this case, C# code at the link below for another approach to saving to jpg may be a helpful reference.
-
Hi MartyC,
Yeah, that's true. I have asked that in the past, but in the pas I was looking for distances.
I have noticed that the CopyTo() method can copy to a byte array [stride * height] if I want the image bytes for rgb and also can copy to ushort array [width * height] if I want to get raw distance matrix.
Is that correct?
Regarding the link you have send it seems to be similar. It's using Marshal.Copy instead of CopyTo(), but it's using directly Jpeg encoder instead of WritableBitmat I'll try it, I supose it is a better way.
With regards,
-
The link below may help with your CopyTo() / ushort question.
-
I have tried both ways to save Jpeg that you have send me.
I didn't know about ImageSharp but it looks like it's a very good library to work with images.
I have used Stopwath() to see if it's better than my way but it is slower.
Marshall way is similar to my way but don't have rotation option (easy) so maybe my way it's good to save images.
Please sign in to leave a comment.
Comments
4 comments