hello I have IsolatedStorageFile and this file is image so I want to upload this image to the serverside for the use of Avatar so how I can upload my byte[] of image.
my code like this.
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
// Open the file - error handling omitted for brevity
// Note: If the image does not exist in isolated storage the following exception will be generated:
// System.IO.IsolatedStorage.IsolatedStorageException was unhandled
// Message=Operation not permitted on IsolatedStorageFileStream
using (IsolatedStorageFileStream isfs = isf.OpenFile("myImage.jpg", FileMode.Open, FileAccess.Read))
{
// Allocate an array large enough for the entire file
data = new byte[isfs.Length];
// Read the entire file and then close it
isfs.Read(data, 0, data.Length);
isfs.Close();
}
}
// Create memory stream and bitmap
MemoryStream ms = new MemoryStream(data);
BitmapImage bi = new BitmapImage();
// Set bitmap source to memory stream
bi.SetSource(ms);
how I can upload this byte[] into webClient or HttpwebRequest.?
Thank you.!
Have you checked out the following link: File Upload. This should work.
Related
I am using the following sample to resize the uploaded images with Blazor WebAssembly
https://www.prowaretech.com/Computer/Blazor/Examples/WebApi/UploadImages .
Still I need the original file too to be converted to base64 too and I don't know how can I access it...
I tried to find the file's original width and height to pass its to RequestImageFileAsync function but no success...
I need to store both files : the original one and the resized one.
Can you help me, please ?
Thank You Very Much !
The InputFile control emits an IBrowserFile type. RequestImageFileAsync is a convenience method on IBrowserFile to resize the image and convert the type. The result is still an IBrowserFile.
One way to do what you are asking is with SixLabors.ImageSharp. Based on the ProWareTech example, something like this...
async Task OnChange(InputFileChangeEventArgs e)
{
var files = e.GetMultipleFiles(); // get the files selected by the users
foreach(var file in files)
{
//Original-sized file
var buf1 = new byte[file.Size];
using (var stream = file.OpenReadStream())
{
await stream.ReadAsync(buf1); // copy the stream to the buffer
}
origFilesBase64.Add(new ImageFile { base64data = Convert.ToBase64String(buf1), contentType = file.ContentType, fileName = file.Name }); // convert to a base64 string!!
//Resized File
var resizedFile = await file.RequestImageFileAsync(file.ContentType, 640, 480); // resize the image file
var buf = new byte[resizedFile.Size]; // allocate a buffer to fill with the file's data
using (var stream = resizedFile.OpenReadStream())
{
await stream.ReadAsync(buf); // copy the stream to the buffer
}
filesBase64.Add(new ImageFile { base64data = Convert.ToBase64String(buf), contentType = file.ContentType, fileName = file.Name }); // convert to a base64 string!!
}
//To get the image Sizes for first image
ImageSharp.Image origImage = Image.Load<*imagetype*>(origFilesBase64[0])
int origImgHeight = origImage.Height;
int origImgWidth = origImage.Width;
ImageSharp.Image resizedImage = Image.Load<*imagetype*>(filesBase64[0])
int resizedImgHeight = resizedImage.Height;
int resizedImgWidth = resizedImage.Width;
}
I am using SignaturePad Nuget for PCL.
How do i convert this System.IO.Stream into an Image?
Any idea would be appreciated.. Thank you.
Here's what I found after 2 weeks of research about this.
This is the code in c# to save an signature to a png file on android phone.
async void Enregistrer(object sender, EventArgs e)
{
// This is the code to get the image (as a stream)
var img = padView.GetImage(Acr.XamForms.SignaturePad.ImageFormatType.Png); // This returns the Bitmap from SignaturePad.
img.Seek(0,0); // This is to have the current position in the stream
// create a new file stream for writing
FileStream fs = new FileStream(Android.OS.Environment.ExternalStorageDirectory + "/imagefilename.png", FileMode.Create,FileAccess.Write); // create a new file stream for writing
await img.CopyToAsync(fs); // have the image stream to disk
fs.Close(); // close the filestream
img.Dispose(); // clear the ressourse used by the stream
}
have fun !
I'm receiving an image on a Metro app through network socket every 1 second, loading it in an array of bytes, then convert it to a BitmapImage and display it later. All of this work fine.
The image is changing constantly on the other side. For some reason, it throws an OutOfMemory exceptions from now and then(like 1 in 10) . I fixed it by clearing the array of bytes every time the image is received. Now it works like charm.
See below for my main issue:
public static BitmapImage imag;
public static byte[] save = new byte[1];
if(recieved)
{
await reader.LoadAsync(4);
var sz = reader.ReadUInt32(); //read size
await reader.LoadAsync(sz); //read content
save = new byte[sz];
reader.ReadBytes(save);
await ImgSrcFromBytes(save)
Array.Clear(save, 0, save.Length); //issue here !!
}
public async Task<ImageSource> ImgSrcFromBytes(byte[] a)
{
imag = new BitmapImage();
var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
await stream.WriteAsync(a.AsBuffer());
stream.Seek(0);
imag.SetSource(stream);
return imag;
}
Now, i'm implementing a new function to save the image as a file if requested by the user with the code below, however, if i clear the array of bytes above, i get an unreadable image, but if i don't clear the array, i get a perfect image.
Note that no exceptions are thrown and both images have the same size.
FileSavePicker picker = new FileSavePicker();
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
picker.SuggestedFileName = "capture.png";
picker.FileTypeChoices.Add("Png File", new List<string>() { ".png" });
StorageFile file = await picker.PickSaveFileAsync();
if (file != null)
{
CachedFileManager.DeferUpdates(file);
await FileIO.WriteBytesAsync(file, save);
await CachedFileManager.CompleteUpdatesAsync(file);
await new Windows.UI.Popups.MessageDialog("Image Saved Successfully !").ShowAsync();
}
I hope i'm clear. It's a trade-off, if i clear the array, i will get no exceptions while receiving streams over sockets, but i won't be able to get a readable image when saving. and vice versa.
I can create the folder in Isolated Storage Explorer, but not write the file into that folder. When I use the code like:
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
store.CreateDirectory("JSON");
using (var isoFileStream = new IsolatedStorageFileStream("JSON\\dd.txt", FileMode.OpenOrCreate, store))
{
using (var isoFileWriter = new StreamWriter(isoFileStream))
{
isoFileWriter.WriteLine(jsonFile);
}
}
only create the folder, but there is no file in that folder. Please give sample code for creating folder in Isolated Storage Explorer and writing a file into that folder. It is a WP7 application.
Did you tried by using the isoFileStream.Write directly instead of using the StreamWriter object isoFileWriter.
Please use following code and try
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
store.CreateDirectory("JSON");
using (var isoFileStream = new IsolatedStorageFileStream("JSON\\dd.txt", FileMode.OpenOrCreate, store))
{
isoFileStream.Write(jsonFile);
}
Try something like this:
// Obtain the virtual store for the application.
IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
iso.CreateDirectory("Database");
// Create stream for the file in the installation folder.
using (Stream input = Application.GetResourceStream(new Uri("test.sdf", UriKind.Relative)).Stream)
{
// Create stream for the new file in the isolated storage
using (IsolatedStorageFileStream output = iso.CreateFile("Database\\test.sdf"))
{
// Initialize the buffer
byte[] readBuffer = new byte[4096];
int bytesRead = -1;
// Copy the file from installation folder to isolated storage.
while((bytesRead = input.Read(readBuffer, 0, readBuffer.Length)) > 0)
{
output.Write(readBuffer, 0, bytesRead);
}
}
}
This code is similar to mine which I use to copy database from application instalation folder to specific folder under isolated storage. Hope it will help you with some inspiration :)
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
store image into isolated storage in windows phone 7
I am using Visual Studio/Expression Blend to create my app for windows phone 7. The user should be able to select a picture that he/she wants to edit and after editing, the user can click a "save" button and the specific edited image will be saved in isolated storage. But I'm having trouble saving the image to Isolated Storage from the button click event.
Does anyone have a code example of how this can be achieved? Thanks!
My codes for the button :
using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
var bi = new BitmapImage(); bi.SetSource(pic);
var wb = new WriteableBitmap(lion.jpg,lion.jpg.RenderTransform);
using (var isoFileStream = isoStore.CreateFile("somepic.jpg"))
{
var width = wb.PixelWidth;
var height = wb.PixelHeight;
Extensions.SaveJpeg(wb, isoFileStream, width, height, 0, 100);
}
}
To save an image to IsolatedStorage from a PhotoChooserTask, use this (the e object in the task callback holds the stream):
public static void SaveImage(Stream imageStream, string fileName, int orientation, int quality)
{
using (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isolatedStorage.FileExists(fileName))
isolatedStorage.DeleteFile(fileName);
IsolatedStorageFileStream fileStream = isolatedStorage.CreateFile(fileName);
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(imageStream);
WriteableBitmap wb = new WriteableBitmap(bitmap);
wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, orientation, quality);
fileStream.Close();
}
}