How to convert Steam string "Xamarin.Forms.StreamImageSource" to image source - xamarin.forms

I am sending image Source to other page like
string bbb = dtnhd.imgsource.ToString();
Other page I got this:
"Xamarin.Forms.StreamImageSource"
How I can change to "Xamarin.Forms.StreamImageSource" to image source.
I am using
byte[] byteArray = Encoding.UTF8.GetBytes(imagepath1);
Stream stream = new MemoryStream(byteArray);
var imageSource = ImageSource.FromStream(() => stream);

Related

Spire.xls - return a File from a stream for client download

Here is what I'm trying to accomplish.
I am creating an asp.net MVC application. My restrictions are that I cannot programmatically save anything to the file structure of the server, so I can't save it as a physical file on the host, and then grab it for client download.
I am loading a PDF to a stream, extracting information from the PDF, dynamically building an excel file, and then offering the file for download to the client. My code is below.
// Loads the incoming PDF document to stream
PdfDocument doc = new PdfDocument();
using (var stream = model.BudgetPdfFile.OpenReadStream())
{
doc.LoadFromStream(stream);
}
var pageCount = doc.Pages.Count;
var date = DateTime.Now.ToShortDateString().Replace("/", "-");
// Extracts data from the PDF and separates it by NewLine
SimpleTextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
StringBuilder allText = new StringBuilder();
for (var i = 0; i < pageCount; i++)
{
allText.Append(doc.Pages[i].ExtractText(strategy));
}
var fullDocText = allText.ToString();
List<string> linesList = new List<string>(fullDocText.Split(new[] { Environment.NewLine }, StringSplitOptions.None).ToList());
// generates a comparison list for output data manipulation from static data
var finalList = linesList.BuildFinalList(budgetItems);
// creates a new Spire.PDF.Workbook for the final output excel file
var result = new Workbook();
// checks for whether the submitted budget is for a case in litigation or not and builds the correct excel workbook
if (model.isTrial)
{
result = ExportExcelBudget.TrialBudgetSheet(model, finalList);
}
else
{
result = ExportExcelBudget.PreTrialBudgetSheet(model, finalList);
}
Absolutely everything up to the last section below works perfectly. However, I cannot figure out how to load the workbook into a new stream and then return the file for download.
// saves the final workbook to a stream and offers it for download to the client
Stream outStream = new MemoryStream();
var fileName = "Budget Report_" + model.ClaimNumber + "_" + date + ".xlsx";
var contentType = "application/vnd.ms-excel";
result.SaveToStream(outStream, Spire.Xls.FileFormat.Version2016);
return File(outStream, contentType, fileName);
I've searched and tried multiple different variations but when the application hits the return File(), it returns a null.
I've stepped through execution and the results seem to be there, but it's not passing anything. Any help on what is wrong here would be greatly appreciated.
Stream outStream = new MemoryStream();
var fileName = "Budget Report_" + model.ClaimNumber + "_" + date + ".xlsx";
var contentType = "application/vnd.ms-excel";
result.SaveToStream(outStream, Spire.Xls.FileFormat.Version2016);
**outStream.Position = 0;**
return File(outStream, contentType, fileName);
Had to reset the stream position to 0. Working perfectly now.

Xamarin Forms: Is it possible to save and retrieve an EpubBook data using SettingsPlugin?

I am using SettingsPlugin for saving values to local DB. Using this plugin I have saved an EpubBook data like below:
CrossSettings.Current.AddOrUpdateValue("epub", epubBook.ToString());
Retrieved the value like below:
string epubString = CrossSettings.Current.GetValueOrDefault("epub", string.Empty);
if (!string.IsNullOrWhiteSpace(epubString))
{
//Need to convert string epub data to EpubBook data
}
I need to convert the string epub data to EpubBook? Is this possible? This is for caching the EpubBook data.
I have done the same using Application.Current.Properties and saving and retrieving is working fine there. Please see below code:
//Saving value
Application.Current.Properties["epub"] = epubBook;
//Retriving value
EpubBook epubBook = (EpubBook)Application.Current.Properties["epub"];
But the Application.Current.Properties values are clearing automatically after closing the app. That's why I start working on SettingsPlugin. So is there any way to save and retrieve an EpubBook using SettingsPlugin? If no suggests a way to caching the EpubBook data?
Answer from my XF thread.
Try to use http client to request the data so we could get bytes directly:
bool hasKey = Preferences.ContainsKey("epub");
if (hasKey)
{
string fileName = Preferences.Get("epub", string.Empty);
var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var filePath = Path.Combine(folderPath, fileName);
var bytes = File.ReadAllBytes(filePath);
// convert
var stream1 = new MemoryStream(bytes);
epubBook = EpubReader.ReadBook(stream1);
}
else
{
Stream stream;
//HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fileUrl);
//HttpWebResponse response = (HttpWebResponse)request.GetResponse();
HttpClient client = new HttpClient();
var response = await client.GetAsync(fileUrl);
//stream = response.GetResponseStream();
stream = await response.Content.ReadAsStreamAsync();
epubBook = EpubReader.ReadBook(stream);
//saving to folder
byte[] bytes = await response.Content.ReadAsByteArrayAsync();
string filename = Path.GetFileName(fileUrl);
var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var filePath = Path.Combine(folderPath, filename);
File.WriteAllBytes(filePath, bytes);
Preferences.Set("epub", filename);
}
We need to add clear text on Android:
<application android:label="EPubDemo.Android" android:usesCleartextTraffic="true"></application>
and disable ATS in iOS ss this request is http.
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

Unable to copy to memorystream from gzipstream

I want to compress a binary file in memory using System.IO.Compression.GZipStream. For this, I am using the following method
public byte[] Encrypt()
{
var payload = GetPayload();
Console.WriteLine("[!] Payload Size: {0} bytes", payload.Length);
using (var compressedStream = new MemoryStream(payload))
using (var zipStream = new GZipStream(compressedStream, CompressionMode.Compress))
using (var resultStream = new MemoryStream())
{
zipStream.CopyTo(resultStream);
return resultStream.ToArray();
}
}
But while .CopyTo, I am getting System.NotSupportedException: Stream does not support reading.
You need to "inverse" your logic: create GZipStream over empty MemoryStream and copy your original content into this gzip stream:
using var compressedData = new MemoryStream();
using var gzip = new GZipStream(compressedData);
originalUncompressedStream.CopyTo(gzip); // <- "magic" happens here
gzip.Flush();
// and "rewind" result stream back to beginning (for next reads)
compressedData.Position = 0;

How to set the stream as ImageSource in Xamarin UWP?

I am having the stream and I trying to set the Image Source as a stream. My code is
var contentPage = new ContentPage();
var image = new Image();
Stream stream = args.Stream;
stream.Position = 0;
image.Source = ImageSource.FromStream(() => stream);
contentPage.Content = image;
Navigation.PushModalAsync(contentPage);
Image rendered in android and iOS but it not worked in UWP. Please suggest me, how to render the image via stream.
Thanks,
Santhiya A
I experienced this also in UWP and have found that you should do this work in the OnAppearing() method of the contentPage.

Saving/Loading Images from/to stream instead of a disk

I have the following code which takes an improperly saved Image from the database converts it to a Jpeg and returns the Image in a byte array;
public Byte[] GetImageFromDB(int id)
{
var imageData = _repository.GetImage(id);
var newImageData = ConvertCorruptedImage(imageData, id);
return newImageData;
}
private byte[] ConvertCorruptedImage(byte[] imageData, int id)
{
// Save DB Image as a file.
MemoryStream img = new MemoryStream(imageData);
var saveDBImage = Image.FromStream(img);
string originalFileName = #"c:\original_" + id.ToString() + ".jpg";
string newFileName = #"C:\new" + id.ToString() + ".jpg";
// Delete if already Exists
DeleteImageFile(originalFileName);
saveDBImage.Save(originalFileName);
// Read Saved DB Image From Saved File & Save as jpeg
Bitmap bm = new Bitmap(originalFileName);
bm.Save(newFileName , ImageFormat.Jpeg);
// Return Converted JPEG Image
var newImage = ImageToByte(Image.FromFile(newFileName));
//DeleteCreatedImage(newFileName);
//DeleteCreatedImage(originalFileName);
return newImage;
}
private byte[] ImageToByte(Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
public static void DeleteImageFile(string fileName)
{
FileInfo file = new FileInfo(fileName);
if (file.Exists && !file.IsReadOnly)
{
System.IO.File.Delete(fileName);
}
}
I was wondering if there was a way to do this without saving a file to the hard disk or if i do save it then deleting it once i am done with it.
I've tried adding a delete for each images (check the commented out portion of the ConvertCorruptedImage method) but i keep getting the following error:
The process cannot access the file 'C:\new_xx.jpg' because it is being used by another process.
I really don't want to be saving images to a hard disk.
Thanks in advance
something along the lines of
var image = Image.FromStream(new MemoryStream(imageData));
Bitmap bmp = new Bitmap(image);
MemoryStream outStream = new MemoryStream();
bmp.Save(outStream,ImageFormat.Jpeg);
return outStream.ToArray();
Use the overload of Bitmap.Save that writes to a Stream.
var stream = new MemoryStream();
bm.Save(stream, ImageFormat.Jpeg);
You can load the bitmap directly from your MemoryStream:
Bitmap bm = new Bitmap(imgStream);
You can also save the bitmap to a stream:
MemoryStream newImgStream = new MemoryStream();
bm.Save(newMemoryStream, ImageFormat.Jpeg);

Resources