I would like to create a stream from a bitmap. First I did it with :
Stream imgStream = typeof(Main).GetTypeInfo().Assembly.GetManifestResourceStream("ApplicationBlockBase.Assets.testimage.jpg");
But now I need to create a Stream from a local image. Do you have any ideas?
Thanks for your time, regards.
Let me know it this works or not.
StorageFile imageFile = await (await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets")).GetFileAsync("testimage.jpg");
Stream imgStream = await imageFile.OpenStreamForReadAsync();
Related
I have a WPF project and captured an image from a usb camera to a System.Drawing.Bitmap (I also can capture System.Windows.Media.Imaging.BitmapSource) and need to convert it to a Windows.Graphics.Imaging.SoftwareBitmap to make a "VideoFrame" to compare to an Onnx model.
The camera driver is a .net assembly and will not bind to a uwp project. I've tried creating a .net standard assembly to bridge the gap with no success. I simply need a bitmap converted to a SoftwareBitmap. Please help!
I'm using this code for the basis of the compassion of the Bitmap image from the camera - https://github.com/Azure-Samples/cognitive-services-onnx12-customvision-sample
There is no direct conversion. You'll need to extract the image data from the System.Drawing.Bitmap and then create the new SoftwareBitmap from that data.
For example, you could use Save(Stream, ImageFormat) method to save this image to the specified stream in the specified format.
Then, you could try to call BitmapDecoder.CreateAsync method to create the decoder from the stream.
After that you could call GetSoftwareBitmapAsync to get a SoftwareBitmap object.
The following is a simple code sample:
Bitmap bitmap = getyourbitmap();
using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
{
bitmap.Save(stream.AsStream(),ImageFormat.Jpeg);//choose the specific image format by your own bitmap source
Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();
}
I found that you can use Windows.Security.Cryptography to create a IBuffer from the image array bytes. Then you can copy the IBuffer to the SoftwareBitmap.
using Windows.Security.Cryptography;
IBuffer buffer = CryptographicBuffer.CreateFromByteArray(ImageByteArray);
SoftwareBitmap softwareBitmap = new SoftwareBitmap(BitmapPixelFormat.Gray8, 800, 600);
softwareBitmap.CopyFromBuffer(buffer);
VideoFrame inputImage = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap);
This worked for me:
Bitmap bitmap = ...;
var memoryStream = new MemoryStream();
using (var graphics = Graphics.FromImage(bitmap))
{
bitmap.Save(memoryStream, ImageFormat.Bmp);
}
var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
var bitmap = await decoder.GetSoftwareBitmapAsync();
In the apirefference it says that the source cannot be a InMemoryRandomAccessStream or any other writeable stream. But i need to transcode a InMemoryRandomAccessStream. I tried converting the Stream but it didnt work:
InMemoryRandomAccessStream untranscodedVideo = GetStream();
Stream source = untranscodedVideo.AsStreamForRead();
await transcoder.PrepareStreamTranscodeAsync(source.AsRandomAccessStream(),destinatiom,profile);
InMemoryRandomAccessStream untranscodedVideo = GetStream();
IOutputStream source = untranscodedVideo.GetOutputStreamAt(0);
await transcoder.PrepareStreamTranscodeAsync(source,destinatin,profile);
But i need to transcode a InMemoryRandomAccessStream. I tried converting the Stream but it didnt work.
The source parameter of PrepareStreamTranscodeAsync is IRandomAccessStream. For this request, you could use CloneStream method to converter InMemoryRandomAccessStream to IRandomAccessStream.
IRandomAccessStream irSteam = stream.CloneStream();
Is there any way of converting Stream to Image?
I tried Bitmap, but it states that I don't have System.Drawing... so I tried this:
var bitMap = new BitmapImage();
bitMap.SetSourceAsync(stream);
image.Source = bitMap;
EDIT:
I am trying to build UWP app + using VS 2015.
2 - It just states that System.Drawing does not exist in the namespace.
EDIT2:
Ok, I might have explained it wrong. The idea is: I have an Image, and I want to change its source to something different and then for it to reload, so I can see the image.
The image is effectively a "Stream", so I assume I need to convert it to Bitmap and then load somehow.
EDIT3:
Ok, so I think it will be easier to describe and then use the code above:
There is a picture box and I am using:
var stream = new InMemoryRandomAccessStream();
await mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream);
Now I would like this Captured Photo to be displayed as an Image. ( Image "box" is created at the start, so the idea is to change source).
Right, so I managed to fix it:
var bitMap = new BitmapImage();
stream.seek(0); // LINE ADDED
bitMap.SetSourceAsync(stream);
image.Source = bitMap;
I turned out that the error that was being produced was : "The component cannot be found.", so I managed to fix it by using this trick.
I am not sure if this is what you are looking for but if you would like to use stream with BitMapImage you should use:
var image = new BitmapImage();
await image.SetSourceAsync(stream);
For instance when you have your photo stored as a byte[] array you can use the stream to convert it to image:
using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
{
DataWriter writer = new DataWriter(stream.GetOutputStreamAt(0))
writer.WriteBytes(<<here your byte[] array>>);
await writer.StoreAsync();
var image = new BitmapImage();
await image.SetSourceAsync(stream);
}
Is that what you need?
I have been discussing this topic on another forum and no one could actually help, let's see if someone here can.
I Have a Problem with the URI you get from the Soundcloud Api to play a song. On my dekstop this Command:
mediaelement.Source = new Uri("https://api.soundcloud.com/tracks/218090824/stream?client_id=YOUR_CLIENT_ID");
mediaelement.Play();
works just fine, on the PI it doesn't at all, no clue why.
I have a workaround for now which looks like this:
Uri turi = new Uri("https://api.soundcloud.com/tracks/218090824/stream?client_id=YOUR_CLIENT_ID");
IRandomAccessStreamReference rsr = RandomAccessStreamReference.CreateFromUri(turi);
PBar.Visibility = Windows.UI.Xaml.Visibility.Visible;
StorageFile file1 = await StorageFile.CreateStreamedFileFromUriAsync("test mp3", turi, rsr);
IRandomAccessStream stream = await file1.OpenReadAsync();
PBar.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
mediaElement.PosterSource = img;
mediaElement.SetSource(stream, "audio/mp3");
mediaElement.Play();
The Problem here is that the files are being downloaded before they are played, this is not a problem if the songfile is <10 mb but with mixtapes >100 mb this takes a long time. So Is it somehow possible to get an IRandomAccessStream from the URI which can be played while being downloaded?
This Solution:
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync("https://api.soundcloud.com/tracks/218090824/stream?client_id=YOUR_CLIENT_ID", HttpCompletionOption.ResponseHeadersRead);
HttpResponseMessage rs = await httpClient.GetAsync(response.RequestMessage.RequestUri, HttpCompletionOption.ResponseHeadersRead);
Stream stream = await rs.Content.ReadAsStreamAsync();
IRandomAccessStream content = stream.AsRandomAccessStream();
mediaElement.SetSource(content, "audio/mpeg");
doesn't work because I get an error when i want to convert the Stream to IRandomAccessStream which says: ""Cannot use the specified Stream as a Windows Runtime IRandomAccessStream because this Stream does not support seeking."
Issue has been resolved with the latest Windows IOT Update.
I'm using Visual studio 2012, c#, silverlight, windows phone 8 app.
We get our data from a webservice, and through the webservice we get a picture that is an base64 string.
I convert it to a byte array, and now I want to save it so the Storage of the windows phone, using a memory stream? I don't know if it is the right approach. I don't want to save it to isolated storage, just the local folder because I want to show the picture after a person tapped on the link.
this is what I have so far.
byte[] ImageArray;
var image = Attachmentlist.Attachment.ToString();
imagename = Attachmentlist.FileName.ToString();
ImageArray = Convert.FromBase64String(image.ToString());
StorageFolder myfolder = Windows.Storage.ApplicationData.Current.LocalFolder;
await myfolder.CreateFileAsync(imagename.ToString());
StorageFile myfile = await myfolder.GetFileAsync(imagename.ToString());
MemoryStream ms = new MemoryStream();
so after I have initialized the memory stream how do I take the byte array and write it to the storage file, and after that retrieve it again?
To write file to disc try this code:
StorageFile sampleFile = await myfolder.CreateFileAsync(imagename.ToString(),
CreateCollisionOption.ReplaceExisting);
await FileIO.WriteBytesAsync(sampleFile, ImageArray);
Memory stream creates stream that writes in memory so it is not applicable to this problem.
StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile imageFile = await folder.CreateFileAsync("Sample.png", CreationCollisionOption.ReplaceExisting);
using (IRandomAccessStream fileStream = await imageFile.OpenAsync(FileAccessMode.ReadWrite))
{
using (IOutputStream outputStream = fileStream.GetOutputStreamAt(0))
{
using (DataWriter dataWriter = new DataWriter(outputStream))
{
dataWriter.WriteBytes(imageBuffer);
await dataWriter.StoreAsync();
dataWriter.DetachStream();
}
//await outputStream.FlushAsync();
}
//await fileStream.FlushAsync();
}