I want to set an image choosen from picture library as background. So I take the originalname of the choosen photo in IsolatedStorageSetting. But later i do not manage to get the stream of the file from the path.. here the code:
bitmapimage.UriSource = new Uri(Settings.BackgroundPhotoUrl, UriKind.Absolute);
BackgroundImg.ImageSource = bitmapimage;
But this code does not work. No exception. Just the background is black.
So I have tried to save the Stream in IsolatedStorageSetting (I do not relly like this solution!!) but in this case I obtain an exception:
Operation denied
Here the code:
Settings.MyStream = e.ChosenPhoto
In the end, I have tried to save the image in isolated storage:
using (System.IO.IsolatedStorage.IsolatedStorageFile isf = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
{
isf.CopyFile(e.OriginalFileName, "background" + System.IO.Path.GetExtension(e.OriginalFileName), true);
}
But also in this case i obtain an operation denied exception
How can I solve the problem?? Thanx
It seems you are misunderstanding streams. A stream is a pointer to a position in a file you can read from or write to. If you are you are using a photo chooser then the result will give you a stream to the file. You need to read the bytes from that at stream and save then you your local storage. Then you can access the image from there.
In my Live Countdown app I use the WriteableBitmap class to save the Jpeg to a stream. Something along the lines of this:
var store =
System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
var newPath = "MyFileName.png";
if (store.FileExists(newPath)) store.DeleteFile(newPath);
var stream = store.CreateFile(newPath);
BitmapImage i = new BitmapImage();
i.SetSource(photoResult.ChosenPhoto);
WriteableBitmap imageToSave = new WriteableBitmap(i);
imageToSave.SaveJpeg(stream, 173, 173, 0, 100);
stream.Flush();
stream.Close();
That is kind of the flow. I had to take parts from different functions and put them together as the app allows the user to scale the app first. There is scaling the SaveJpeg method parameters as I am saving the image for a tile.
Related
I have a video file that's already loaded in memory (an IFormFile that's converted to a byte[]).
I've been trying to figure out how to take that video and get a thumbnail image from it, without having to read/write to disk.
I found use cases for MediaToolkit and FFmpeg here, and Movie Thumbnailer here, but from what I can find, those require the video already be saved to disk and have write access to output the thumbnail to a file.
Is there any way I can take either an IFormFile or byte[] and do something similar to what MediaToolkit is doing while being able to keep the result in memory?
I know a lot of folks are saying byte[] isn't the way to go. In that case, I'm more than happy to convert from IFormFile to a Stream, but I still need a way to do that and keep it in memory.
please try this with Windows.Media.Editing should work, not fully tested..
int frameHeight; // you can set the height
int frameWidth; // ...
TimeSpan getFrameInTime = new TimeSpan(0, 0, 1);
//Using Windows.Media.Editing get your ImageStream
var yourClip = await MediaClip.CreateFromFileAsync(someFile);
var composition = new MediaComposition();
// add the section of the video as needed
composition.Clips.Add(yourClip);
// Answer - now that your have your imageStream, get the thumbnail
var yourImageStream = await composition.GetThumbnailAsync(
getFrameInTime,
Convert.ToInt32(frameWidth),
Convert.ToInt32(frameHeight),
VideoFramePrecision.NearestFrame);
//now create your thumbnail bitmap
var yourThumbnailBitmap = new WriteableBitmap(
Convert.ToInt32(frameWidth),
Convert.ToInt32(frameHeight)
);
yourThumbnailBitmap.SetSource(yourImageStream);
I'm attempting to save a bitmap file as a jpeg into Azure without saving it locally in the process, so I am first saving the bitmap as a jpeg in a MemoryStream.
But when I execute the following code, the file uploads but it did not convert the bitmap correctly. If I view the file, the viewer displays 'Invalid Image.'
I read somewhere that bitmaps cannot be converted to jpegs in memory. Could that be what is happening here?
// Retrieve reference to a blob
var blobContainer = GetBlobContainer(Properties.Settings.Default.BlobContainerName);
var blob = blobContainer.GetBlockBlobReference(blobFilePath);
// Save bitmap to jpeg in MemoryStream, then upload to Azure blob
//var writer = new StreamWriter(blob.OpenWrite());
MemoryStream memStr = new MemoryStream();
bitmap.Save(memStr, System.Drawing.Imaging.ImageFormat.Jpeg);
blob.UploadFromStream(memStr);
After writing to the MemoryStream you need to "rewind" it by setting memStr.Position = 0 before any attempts to read it (in your case, uploading it to Azure)
"Be kind, please rewind."
I would like to be able to resample a jpeg loaded into memory from the local network and then save the resulting bitmap file as a jpeg directly to the Azure cloud (without first having to save the jpeg to the local network then uploading). I cannot find any methods for doing so, but I thought I would check here JIC. Here's my high-level code, which is missing the part where the bitmap is saved to the blob as a jpeg:
Bitmap img = ImageMethods.ResampleImage(Image.FromFile(imageFileNames(i)), ImageMethods.ImageSize.Small_800x600)
var blobContainer = GetBlobContainer(Properties.Settings.Default.BlobContainerName);
var blob = blobContainer.GetBlockBlobReference(blobFilePath);
// Save image into Azure as jpeg...
You can create a StreamWriter using
var writer = new StreamWriter(blob.OpenWrite());
Then, you just need to save into this StreamWriter.
I am working on a Windows Store app for Windows 8.1, and I can't find anything that addresses what I need to do. I am trying to save an image of a pdf page to my local app data storage. I have the following:
private async void GetFirstPage()
{
// Retrieve file from FutureAccessList
StorageFile file = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFileAsync(token);
// Truncate last 4 chars; ex: '.pdf'
FileName = file.Name.Substring(0, file.Name.Length - 4);
// Get access to pdf functionality from file
PdfDocument doc = await PdfDocument.LoadFromFileAsync(file);
// Get a copy of the first page
PdfPage page = doc.GetPage(0);
// Below could be used to tweak render options for optimizations later
//PdfPageRenderOptions options = new PdfPageRenderOptions();
// Render the first page to the BitmapImage
InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
await page.RenderToStreamAsync(stream);
// Common code
Cover.SetSource(stream);
// Convert the active BitmapImage Cover to a storage file to be stored locally
// ???
}
The variable, Cover, is a BitmapImage that is bound in XAML to display the image to the user. I want to save this image to my local app data so that I don't have to re-render it through the PDF library every time my app opens! The problem is I can't save anything in a Windows Store app from a stream unless it is text to my knowledge, (no filestream or fileoutputstreams for me), and Cover's URI source is null, since it was sourced from a stream, making it difficult to get my BitmapImage, Cover, to a StorageFile for proper Windows Store saving.
Everything works for me currently, I'm just upset about re-rendering the pdf page to my bitmapimage from scratch every time my app opens. Thanks in advance for any input!
You need to save the image from the PdfPage directly rather than from the BitmapImage since you can't get the data out of the BitmapImage. The PdfPage.RenderToStreamAsync already encodes the stream as a bitmap so you don't need to do any manipulation beyond sending it to the file. This is essentially the same as what you are doing to save to the InMemoryRandomAccessStream, except to a file based stream:
//We need a StorageFile to save into.
StorageFile saveFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("safedPdf.png",CreationCollisionOption.GenerateUniqueName);
using (var saveStream = await saveFile.OpenAsync(FileAccessMode.ReadWrite))
{
await page.RenderToStreamAsync(saveStream);
}
I have a functioning application in c#/.net that currently accepts raw image data in a bayer format from a set of embedded cameras and converts them to jpeg images. To save transmission time, I have modified the embedded devices to encode the images as jpegs prior to transmission. I'm an experienced embedded programmer but a total c#/.net noob. I have managed to modify the application to save the arrays to file with a jpeg name using this snippet: ( the offset of 5 is to skip header data in the transmission frame)
FileStream stream = File.Create(fileName);
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(multiBuff.msgData, 5, multiBuff.dataSize - 5);
writer.Close();
The files open up fine, but now I want to treat the data as a bitmap without having to save & load from file. I tried the following on the data array:
MemoryStream stream = new MemoryStream(data);
BinaryReader reader = new BinaryReader(stream);
byte[] headerData = reader.ReadBytes(5);
Bitmap bmpImage = new Bitmap(stream);
But this throws a parameter not valid exception. As a newbie, I'm a little overwhelmed with all the classes and methods for images and it seems like what I'm doing should be commonplace, but I can't find any examples in the usual places. Any ideas?
I think you are looking for Bitmap.FromStream() :
Bitmap bmpImage = (Bitmap)Bitmap.FromStream(stream);
Actually using new Bitmap(stream) should have worked as well - this means that the data in the stream does not constitute a valid image - are you sure the jpg is valid? Can you save it to disk and open it i.e. in Paint to test?
You use the Image class.
Image image;
using (MemoryStream stream = new MemoryStream(data))
{
image = Image.FromStream(stream);
}
FYI it didn't work because reader.ReadBytes(5) returns the 5 first bytes of stream not the bytes after position 5