How can I convert a WriteableBitmap to stream on WP8 platform?
Final goal is to dump the image output produced by native code to the CameraRoll library using SavePictureToCameraRoll(filename, stream)
You can encode your bitmap to stream with WritableBitmap.SaveJpeg method and use this stream as a parameter to MediaLibrary.SavePictureToCameraRoll.
Note: before calling MediaLibrary.SavePictureToCameraRoll don't forget to set the stream position to 0 if you're using a MemoryStream. Like this:
var wb = new WriteableBitmap(bitmap);
var fileStream = new MemoryStream();
wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 100, 100);
fileStream.Seek(0, SeekOrigin.Begin);
var m = new MediaLibrary();
m.SavePictureToCameraRoll("test", fileStream);
Related
I am using this example to load a FLV file to my project, it's very simple. It works, but I would like to get the bytes (as ByteArray) after I load the video. Is there any way I can perform this procedure?
For convenience, I was using the File object (Adobe AIR) to load the file, but have not found a way to convert the bytes to a Video object.
Does anyone have any idea how I can load a video file and, after loading, get the ByteArray of this object?
var connection:NetConnection = new NetConnection();
connection.connect(null);
var stream:NetStream = new NetStream(connection);
var client:Object = {};
client.onMetadata = function():void{};
stream.client = client;
var video:Video = new Video();
addChild(video);
video.attachNetStream(stream);
stream.play(null);
stream.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
var file:File = new File("path/to/your/video.flv");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ);
// this gives you access to the raw bytes of the file
var bytes:ByteArray = new ByteArray();
fileStream.readBytes(bytes);
// this adds the bytes to the NetStream and begins playback
stream.appendBytes(bytes);
I try to play a ogg music file using NVorbis to decode it, and sharpdx + XNA SoundEffect
Vorbis decoder seems to work as the informations (channels, duration, SampleRate) are fine.
then I use Sharpdx to create the wave Stream that will be used to create the SoundEffect.
But when I try to play it, I can recognise my sound, but there also a lot of "noise" in background and the music is distorted.
I don't know what to do... thanks for help!
here is my code:
string _audioPath = "music/2test.ogg";
NVorbis.Ogg.ContainerReader oggReader = new NVorbis.Ogg.ContainerReader(_audioPath);
using (var vorbis = new NVorbis.VorbisReader(oggReader))
{
int channels = vorbis.Channels;
int sampleRate = vorbis.SampleRate;
var duration = vorbis.TotalTime;
var buffer = new float[vorbis.TotalSamples];
int count;
count = vorbis.ReadSamples(buffer, 0, buffer.Length);
//var byteArray = new byte[buffer.Length * 4];
// Buffer.BlockCopy(buffer, 0, byteArray, 0, byteArray.Length);
MemoryStream stream = new MemoryStream();
// BinaryWriter writer = new BinaryWriter(stream);
SharpDX.Multimedia.WaveFormat wf = new SharpDX.Multimedia.WaveFormat(sampleRate, channels);
SharpDX.Multimedia.WavWriter w = new SharpDX.Multimedia.WavWriter(stream);
w.Begin(wf);
w.AppendData(buffer);
w.End();
stream.Position = 0;
_SoundEffect = SoundEffect.FromStream(stream);
_SoundEffectInstance = _SoundEffect.CreateInstance();
_SoundEffectInstance.Play();
}
i am trying to save an image i.e., present in my project to the isolated storage. i tried using the following code but it is throwing System.NullReferenceException.
IsolatedStorageFileStream fileStream1 = myIsolatedStorage.CreateFile("shared/shellcontent/image.jpg");
Uri uri = new Uri(#"/myapp;/component/contact.bmp", UriKind.Relative);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.UriSource = uri;
WriteableBitmap wb = new WriteableBitmap(bitmapImage); //throwing exception
Extensions.SaveJpeg(wb, fileStream1, wb.PixelWidth, wb.PixelHeight, 0, 85);
i tried with jpg image also. copy to output directory set to copy if never. also tried with build action set to content
.............
just tried this code and the exception isn't showing now but still when am setting it to a tile background. its blank
Uri uri = new Uri("contact.jpg", UriKind.Relative);
StreamResourceInfo sri = null;
sri = Application.GetResourceStream(uri);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(sri.Stream);
WriteableBitmap wb = new WriteableBitmap(bitmapImage);
Extensions.SaveJpeg(wb, fileStream1, wb.PixelWidth, wb.PixelHeight, 0, 85);
update:
issue is resolved. filestream was not closed. silly me
I have stored few images in isolatedstorage and I am trying to replace them by using
using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication()){
if (isStore.FileExists(fileName)){
isStore.DeleteFile(fileName);
}
using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write)){
// Initialize the buffer for 4KB disk pages.
byte[] readBuffer = new byte[4096];
int bytesRead = -1;
// Copy the thumbnail to the local folder.
while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0){
targetStream.Write(readBuffer, 0, bytesRead);
targetStream.Close();
}
}
Now When I am trying to access the new file I end up seeing the old photo. the new photo is not replaced immediately.
But when I close the app and again fetch it I get the new photo. What is wrong?
I was using an ImageBrush to paint the background grid and was binding only the ImageSource of the ImageBrush. I guess this ImageBrush was not getting updated so instead of changing the source of the ImageBrush I created a new object of it and assigned it to Grid.Background. so now it works :)
I'm trying to save an image from the web to the isolated storage from a background task but it throws
An unhandled exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll
i'm using this piece of code
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (myIsolatedStorage.FileExists(tempJPEG))
{
myIsolatedStorage.DeleteFile(tempJPEG);
}
IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(tempJPEG);
StreamResourceInfo sri = null;
Uri uri = new Uri(tempJPEG, UriKind.Relative);
sri = Application.GetResourceStream(uri);
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(e.Result);
WriteableBitmap wb = new WriteableBitmap(bitmap);
// Encode WriteableBitmap object to a JPEG stream.
Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 100);
//wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
fileStream.Close();
}
This works 100% when it's runing from the app , but not from the background task.
Any tips on how to save the image ?
You need to invoke this code Dispatcher.BeginInvoke() since WriteableBitmap needs to execute on the UI thread and not a background thread. See # http://codeblog.vurdalakov.net/2012/02/solution-wp7-unauthorizedaccessexceptio.html