Out of memory exception in WP7 - windows-phone-7.1.1

When I am trying to save some images to my IsolatedStorage there is an Out of Memory exception occures. If the image count is more than 20 this error occurs. I am downloading all these images and save them in a temp folder in isolated storage. When I am trying to save these images from temp folder to a folder named myImages in isolated storage, this error occurs. Each photo is read from temp and write to myImages one by one. When some 20 or 25 photos are saved to myImages then this error occures. Images has an average size of 350-400 KB. How can I avoid this error?
My Code is :
private void SaveImages(int imageCount)
{
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
BitmapImage bitmap;
string tempfoldername = "Temp";
string tempfilename = string.Empty;
string folderName = "myImages";
string imageName = string.Empty;
for (int i = 0; i < imageCount; i++)
{
tempfilename = tempfoldername + "\\" + (i + 1) + ".jpg";
bitmap = GetImage(tempfoldername, tempfilename);
imageName = folderName + "\\" + (i + 1) + ".jpg";
SaveImage(bitmap, imageName, folderName);
if (isf.FileExists(imageName))
isf.DeleteFile(imageName);
bitmap = null;
}
}
private BitmapImage GetImage(string foldername, string imageName)
{
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream isfs;
BitmapImage bi = new BitmapImage();
MemoryStream ms = new MemoryStream();
byte[] data;
int FileSize = 0;
if (isf.DirectoryExists(foldername))
{
isfs = isf.OpenFile(imageName, FileMode.Open, FileAccess.Read);
data = new byte[isfs.Length];
isfs.Read(data, 0, data.Length);
ms.Write(data, 0, data.Length);
FileSize = data.Length;
isfs.Close();
isfs.Dispose();
bi.SetSource(ms);
ms.Dispose();
ms.Close();
return bi;
}
return null;
}
private void SaveImage(BitmapImage bitmap, string imageName, string folderName)
{
int orientation = 0;
int quality = 100;
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!isf.DirectoryExists(folderName))
isf.CreateDirectory(folderName);
if (isf.FileExists(imageName))
isf.DeleteFile(imageName);
Stream fileStream = isf.CreateFile(imageName);
WriteableBitmap wb = new WriteableBitmap(bi);
wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, orientation, quality);
fileStream.Close();
}
}
how can I fix this error?

Your BitmapImage is most likely leaking the memory.
Be sure to set the UriSource to null so that the memory is freed.
Have a look at http://blogs.msdn.com/b/swick/archive/2011/04/07/image-tips-for-windows-phone-7.aspx for more.

Related

Displaying image from database in iTextSharp document [duplicate]

I try to create a PDF report from a datatable. One of the columns contents image. How can I extract the image from datatable and insert into PDF table? I'm using iTextShap version 5.4.2.0. Here is the code:
public void Report(DataTable dt, string output)
{
Document doc = new Document(PageSize.LETTER, 50, 50, 80, 50);
PdfWriter PDFWriter = PdfWriter.GetInstance(doc, new FileStream(output, FileMode.Create));
PDFWriter.ViewerPreferences = PdfWriter.PageModeUseOutlines;
iTextSharp.text.Font hel8 = FontFactory.GetFont(BaseFont.HELVETICA, 8);
doc.Open();
PdfPTable table = new PdfPTable(dt.Columns.Count);
float[] widths = new float[] { 1.2f, 1.2f, 1.2f, 1.2f, 1f, 4f, 1f, 4f };
table.SetWidths(widths);
table.WidthPercentage = 100;
PdfPCell cell = new PdfPCell(new Phrase("NewCells"));
cell.Colspan = dt.Columns.Count;
foreach (DataColumn c in dt.Columns)
{
table.AddCell(new Phrase(c.ColumnName, hel8));
}
foreach (DataRow r in dt.Rows)
{
if (dt.Rows.Count > 0)
{
table.AddCell(new Phrase(r[0].ToString(), hel8));
table.AddCell(new Phrase(r[1].ToString(), hel8));
table.AddCell(new Phrase(r[2].ToString(), hel8));
table.AddCell(new Phrase(r[3].ToString(), hel8));
table.AddCell(new Phrase(r[4].ToString(), hel8));
table.AddCell(new Phrase(r[5].ToString(), hel8));
byte[] byt = (byte[])r[6];
MemoryStream ms = new MemoryStream(byt);
System.Drwaing.Image sdi = System.Drawing.Image.FromStream(ms);
Image img = Image.GetInstance(sdi); <-- this is the problem code
table.AddCell(img);
table.AddCell(new Phrase(r[7].ToString(), hel8));
}
}
doc.Add(table);
}
doc.Close();
}
Update: #nekno, all of your suggestions are worked.
But I still need to correct the casting at line:
byte[] byt = (byte[])r[6];
It gave me a casting exception from VS2008. So I added the conversion function (pulled it from stackoverflow):
byte[] ImageToByte(System.Drawing.Image img)
{
byte[] byteArray = new byte[0];
using (MemoryStream stream = new MemoryStream())
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Close();
byteArray = stream.ToArray();
}
return byteArray;
}
And revised the code:
byte[] byt = ImageToByte((System.Drawing.Image)dt.Rows[e][6]);
Thanks.
What exactly is the problem? What happens when you use your problem code?
Try one of the other Image.GetInstance() overloads:
You can pass the byte array directly:
byte[] byt = (byte[])r[6];
Image img = Image.GetInstance(byt);
Or you can pass the Stream:
byte[] byt = (byte[])r[6];
MemoryStream ms = new MemoryStream(byt);
Image img = Image.GetInstance(ms);
Or you can give iTextSharp more info about the image format:
byte[] byt = (byte[])r[6];
MemoryStream ms = new MemoryStream(byt);
System.Drawing.Image sdi = System.Drawing.Image.FromStream(ms);
Image img = Image.GetInstance(sdi, ImageFormat.Png);
If your column can be cast to a System.Drawing.Image, then you can use it directly:
Image img = Image.GetInstance((System.Drawing.Image)r[6], System.Drawing.Imaging.ImageFormat.Png);
I have suggested steps how shows how to add image into PDF, given below code snippet show how to add logo into your PDF using iTextsharp, follow provided below steps:
I have provided link to download "itextsharp" component from given link http://sourceforge.net/projects/itextsharp/
You have to add reference into your application.
Next you have to add required namespaces "iTextsharp.text.html", "iTextsharp.text" to consume its best properties.
Now you have to add code snippet into your application given at the end, add code snippet under "button click" in code behind.
Hope it will work for you !!!
protected void btnPDF_Click(object sender, ImageClickEventArgs e)
{
DataTable dtn = new DataTable();
dtn = GetDataTable();
dtPDF = dtn.Copy();
for (int i = 0; i <= dtn.Rows.Count - 1; i++)
{
ExportToPdf(dtPDF);
}
}
public void ExportToPdf(DataTable myDataTable)
{
Document pdfDoc = new Document(PageSize.A4, 10, 10, 10, 10);
try
{
PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);
pdfDoc.Open();
Chunk c = new Chunk("" + System.Web.HttpContext.Current.Session["CompanyName"] + "", FontFactory.GetFont("Verdana", 11));
Paragraph p = new Paragraph();
p.Alignment = Element.ALIGN_CENTER;
p.Add(c);
pdfDoc.Add(p);
string clientLogo = Server.MapPath(".") + "/logo/tpglogo.jpg";
string imageFilePath = Server.MapPath(".") + "/logo/tpglogo.jpg";
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
//Resize image depend upon your need
jpg.ScaleToFit(80f, 60f);
//Give space before image
jpg.SpacingBefore = 0f;
//Give some space after the image
jpg.SpacingAfter = 1f;
jpg.Alignment = Element.HEADER;
pdfDoc.Add(jpg);
Font font8 = FontFactory.GetFont("ARIAL", 7);
DataTable dt = myDataTable;
if (dt != null)
{
//Craete instance of the pdf table and set the number of column in that table
PdfPTable PdfTable = new PdfPTable(dt.Columns.Count);
PdfPCell PdfPCell = null;
for (int rows = 0; rows < dt.Rows.Count; rows++)
{
for (int column = 0; column < dt.Columns.Count; column++)
{
PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows][column].ToString(), font8)));
PdfTable.AddCell(PdfPCell);
}
}
//PdfTable.SpacingBefore = 15f; // Give some space after the text or it may overlap the table
pdfDoc.Add(PdfTable); // add pdf table to the document
}
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename= SampleExport.pdf");
System.Web.HttpContext.Current.Response.Write(pdfDoc);
Response.Flush();
Response.End();
//HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch (DocumentException de)
{
System.Web.HttpContext.Current.Response.Write(de.Message);
}
catch (IOException ioEx)
{
System.Web.HttpContext.Current.Response.Write(ioEx.Message);
}
catch (Exception ex)
{
System.Web.HttpContext.Current.Response.Write(ex.Message);
}
}

Compress byte[] (Images) using icsharpziplib

Have only one image that is the byte[] bytes and I am trying to compress to Zip for the user to download as far I can see the byte array is saved in the MemoryStream.
The probleam is when I trie to read the buffer of MemoryStream to ImagesAux this way zippedMemoryStream.Read(ImagesAux, 0, 1000000); doesn't work but ImagesAux = zippedMemoryStream.ToArray(); this way allready works ... then stream.Write(ImagesAux, 0, ImagesAux.Length); as to pass the ZipFile to SaveFileDialog doesn't work either get the size of ImagesAux but not the content from what I saw, the ZipFile is created but gives a error of Format not supported in ALZip, can someone point what I'm doing wrong??
MemoryStream zippedMemoryStream = new MemoryStream();
ZipOutputStream zipOutputStream;
zipOutputStream = new ZipOutputStream(zippedMemoryStream, 1000000);
zipOutputStream.SetLevel(0);
zipOutputStream.UseZip64 = UseZip64.On;
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
ZipEntry entry = new ZipEntry(ZipEntry.CleanName(ImageNameServer));//ImageNameServer
entry.DateTime = DateTime.Now;
entry.Comment = "Teste";
entry.ZipFileIndex = 1;
entry.Size = bytes.Length;
zipOutputStream.PutNextEntry(entry);
zipOutputStream.Write(bytes, 0, bytes.Length);
var something = zipOutputStream.GetType();
var lvl = zipOutputStream.GetLevel();
//zippedMemoryStream.Write(bytes, 0, bytes.Length);
zippedMemoryStream.Read(buffer, 0, bytes.Length);
MemoryStream auxxpto = new MemoryStream();
zippedMemoryStream.WriteTo(auxxpto);
buffer = auxxpto.ToArray();
private void DataSetDownload(object sender, RoutedEventArgs e)
{
var dialog = new SaveFileDialog();
dialog.Filter = "Zip Files (*.zip)|*.zip";
dialog.DefaultExt = "zip";
dialog.DefaultFileName = "DataSet.zip";
bool? fileSelected = dialog.ShowDialog();
byte[] ImagesAux = new byte[1000000];
if (fileSelected == true)
{
var value = zippedMemoryStream.CanWrite;
var read = zippedMemoryStream.CanRead;
zippedMemoryStream.Read(ImagesAux, 0, 1000000);
ImagesAux = zippedMemoryStream.ToArray();
//ZippedFile.Read(ImagesAux, 0, ImagesAux.Length);
//ImagesAux = zippedMemoryStream.ToArray();
zipOutputStream.Finish();
zipOutputStream.Close();
using (Stream stream = dialog.OpenFile())
{
stream.Write(ImagesAux, 0, ImagesAux.Length);
//stream = zippedMemoryStream.ToArray();
//stream.Flush();
//stream.Close();
}
}
}
This code was moved from the question. Answers should be in answers.
Working code.
MemoryStream zippedMemoryStream = new MemoryStream();
ZipOutputStream zipOutputStream;
zipOutputStream = new ZipOutputStream(zippedMemoryStream, 1000000);
zipOutputStream.SetLevel(0);
zipOutputStream.UseZip64 = UseZip64.On;
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
ZipEntry entry = new ZipEntry(ZipEntry.CleanName(ImageNameServer));//ImageNameServer
zipOutputStream.PutNextEntry(entry);
zipOutputStream.Write(bytes, 0, bytes.Length);
zipOutputStream.Finish();
zipOutputStream.CloseEntry();
private void DataSetDownload(object sender, RoutedEventArgs e)
{
var dialog = new SaveFileDialog();
dialog.Filter = "Zip Files (*.zip)|*.zip";
dialog.DefaultExt = "zip";
dialog.DefaultFileName = "DataSet.zip";
bool? fileSelected = dialog.ShowDialog();
byte[] ImagesAux = new byte[1000000];
if (fileSelected == true)
{
var value = zippedMemoryStream.CanWrite;
var read = zippedMemoryStream.CanRead;
zippedMemoryStream.Position = 0;
zippedMemoryStream.Read(ImagesAux, 0, 1000000);
//ZippedFile.Read(ImagesAux, 0, ImagesAux.Length);
//ImagesAux = zippedMemoryStream.ToArray();
zipOutputStream.Finish();
zipOutputStream.Close();
using (Stream stream = dialog.OpenFile())
{
stream.Write(ImagesAux, 0, ImagesAux.Length);
//stream = zippedMemoryStream.ToArray();
//stream.Flush();
//stream.Close();
}
}
}

C# Windows Application Image Resizing Before Saving

I have this Code to Save a Pic to my Solution Explorer Images Folder.
private void btnUploadImage_Click(object sender, EventArgs e)
{
//The String used to store the location of the file that is currently loaded in the picture box picFile
String location;
//The String used to store the name of the file that is currently loaded in the picture box picFile
String fileName;
ofdImageUpload.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
//Showing the fileopen dialog box
ofdImageUpload.ShowDialog();
//showing the image opened in the picturebox
imgCapture.Image = new Bitmap(ofdImageUpload.FileName);
//storing the location of the pic in variable
location = ofdImageUpload.FileName;
txtImgLocation.Text = location;
//storing the filename of the pic in variable
fileName = ofdImageUpload.SafeFileName;
//pictureboxImage.Image.Save();
imgCapture.SizeMode = PictureBoxSizeMode.StretchImage;
if (imgCapture.Image != null)
{
lblHiddenMsg.Text = "";
}
}
private void InsertGatepassEntry(int RowId)
{
string ContName = txtContName.Text.Trim();
string ContAdd = richtxtContAddress.Text.Trim();
string VisitorName = txtEmpName.Text.Trim();
string VisitorAdd = txtEmpAddress.Text.Trim();
string VisitorFathersName = txtEmpFatherName.Text.Trim();
string VisitorAge = txtEmpAge.Text.Trim();
string VisitorEsi = txtEsi.Text.Trim();
string VisitorContact = txtEmpContactNo.Text.Trim();
string VisitorBloodGrp = comboxBloodGroup.SelectedText.Trim();
string VisitorIssueDate = dtpEmpDateOfIssue.Text.Trim();
string imagename = ofdImageUpload.SafeFileName;
if (imagename != null || imagename != "") //Check If image is Selected from Computer's Hard Drive
{
if (imgCapture.Image != null)
{
//string imagepath = ofdImageUpload.FileName;
//string picname = imagepath.Substring(imagepath.LastIndexOf('\\'));
string picname = imagename;
string path = Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("bin"));
Bitmap imgImage = new Bitmap(imgCapture.Image); //Create an object of Bitmap class/
//string fullPathName = path + "Images" + picname;
imgImage.Save(path + "Images\\" + txtEmpName.Text + txtEsi.Text + ".jpg");
string Image = "Images\\" + txtEmpName.Text + txtEsi.Text + ".jpg";
string GatepassNo = LoadLastGatepassNo();
switch (Contractor.InsertGatepassEntry(RowId, ContName, ContAdd, VisitorName, VisitorAdd, VisitorFathersName, VisitorAge, VisitorEsi, VisitorContact, VisitorBloodGrp, VisitorIssueDate, Image, GatepassNo))
{
case ProjectCreateStatus.Insertrow:
lblMessage.Text = "Information inserted successfully!";
lblMessage.ForeColor = System.Drawing.Color.Green;
lblGatepassNo.Text = GatepassNo;
break;
}
}
else
{
lblHiddenMsg.Visible = true;
lblHiddenMsg.Text = "Please capture or browse an image First";
}
}
else //image is directly uploding from Webcam Capture
{
string Image = lblHiddenMsg.Text;
string GatepassNo = LoadLastGatepassNo();
switch (Contractor.InsertGatepassEntry(RowId, ContName, ContAdd, VisitorName, VisitorAdd, VisitorFathersName, VisitorAge, VisitorEsi, VisitorContact, VisitorBloodGrp, VisitorIssueDate, Image, GatepassNo))
{
case ProjectCreateStatus.Insertrow:
lblMessage.Text = "Information inserted successfully!";
lblMessage.ForeColor = System.Drawing.Color.Green;
lblGatepassNo.Text = GatepassNo;
break;
}
}
}
private void bntCapture_Click(object sender, EventArgs e)
{
imgCapture.Image = imgVideo.Image;
Bitmap b = new Bitmap(imgCapture.Image);
string path = Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("bin"));
b.Save(path + "Images\\" + txtEmpName.Text + txtEsi.Text + ".jpg");
lblHiddenMsg.Text = path + "Images\\" + txtEmpName.Text + txtEsi.Text + ".jpg";
}
Now I want to Save those uploaded Pic to my Images Folder to a particular Size like 250x250. can Anyone Help ? m new in Windows Application C#.
To resize your image use:
public static Image resizeImage(Image imgToResize, Size size)
{
return (Image)(new Bitmap(imgToResize, size));
}
yourImage = resizeImage(yourImage, new Size(250,250));
hi you can add the "Size" class as a parameter of your Bitmap constructor to resize the image
i did for you this function to make it easy hope that will help you
public static Image resizeImage(Image imgToResize, Size size)
{
return (Image)(new Bitmap(imgToResize, size));
}
yourImage = resizeImage(yourImage, new Size(250,250));

Uploading a file by Console Application asp.net

i've searched about how to upload files to websites using console applications and i reached some ways, seems like the correct way, but i'm not having sucess with this. So i need some help!
First, the solution that i've founded follows:
class Program
{
static void Main(string[] args)
{
//Program.Test1();
Program.Test3();
Console.ReadLine();
}
public static void Test3()
{
//Set this to dont get an Invalid Request Exception
System.Net.ServicePointManager.Expect100Continue = false;
//Set a real page for this test
string url = "http://www.toledorocket.com/perftest/uploadtest/fileselect.asp";
string[] files = { "C:\\Documents and Settings\\wkurten\\Desktop\\fogao.txt" }; //Put some real file
NameValueCollection nvc = new NameValueCollection();
nvc.Add("FILE1", "fogao.txt");
string boundary = "----------------------------" +
DateTime.Now.Ticks.ToString("x");
HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest2.ContentType = "multipart/form-data; boundary=" +
boundary;
httpWebRequest2.Method = "POST";
httpWebRequest2.KeepAlive = true;
httpWebRequest2.Credentials =
System.Net.CredentialCache.DefaultCredentials;
//Is you have an connection with proxy, uncomment and set the values bellow:
/*NetworkCredential localNetworkCredential = new NetworkCredential("user", "pass", "domain");
httpWebRequest2.Proxy = new WebProxy("server:port", false);
httpWebRequest2.Proxy.Credentials = localNetworkCredential;*/
Stream memStream = new System.IO.MemoryStream();
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +
boundary + "\r\n");
string formdataTemplate = "\r\n--" + boundary +
"\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";
foreach (string key in nvc.Keys)
{
string formitem = string.Format(formdataTemplate, key, nvc[key]);
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
memStream.Write(formitembytes, 0, formitembytes.Length);
}
memStream.Write(boundarybytes, 0, boundarybytes.Length);
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";
for (int i = 0; i < files.Length; i++)
{
string header = string.Format(headerTemplate, "file" + i, files[i]);
//string header = string.Format(headerTemplate, "uplTheFile", files[i]);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
memStream.Write(headerbytes, 0, headerbytes.Length);
FileStream fileStream = new FileStream(files[i], FileMode.Open,
FileAccess.Read);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
memStream.Write(buffer, 0, bytesRead);
}
memStream.Write(boundarybytes, 0, boundarybytes.Length);
fileStream.Close();
}
httpWebRequest2.ContentLength = memStream.Length;
Stream requestStream = httpWebRequest2.GetRequestStream();
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
//Gets the response
WebResponse webResponse2 = httpWebRequest2.GetResponse();
Stream stream2 = webResponse2.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
//Retrieve the html from response
string htmlResponse = reader2.ReadToEnd();
webResponse2.Close();
httpWebRequest2 = null;
webResponse2 = null;
}
}
The problem that i'm having with this solution is that when i upload a file and retrieves the WebResponse, the page that i'm getting is the page with the upload form and not the page that appears after upload, that page with the success message.
On the page that i'm trying to upload the file i have this html/form:
<FORM METHOD="POST" ENCTYPE="multipart/form-data" ACTION="uploadstatus.asp">
<INPUT TYPE="FILE" SIZE="40" NAME="FILE1"> <INPUT TYPE=SUBMIT VALUE="Upload!">
</FORM>
When i run my code i just get this same form every time, seems like i'm never posting anything... anyone have an idea of what is happening?

How to save bitmapimage in Isolated storage?

I am developing silverlight web part by using the client object model. I am getting the bitmap image from sharepoint server. Now I want to save this bitmap image in isolated storage. So I am using following code
WriteableBitmap wb = new WriteableBitmap(attachments);
using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isoStream =
new IsolatedStorageFileStream("abcd1.jpg", FileMode.Create, isoFile))
{
using (StreamWriter sw = new StreamWriter(isoStream))
{
sw.Write(wb.ToByteArray());
}
}
}
Now I am seeing the saved image at location C:\Users\Rent2\AppData\LocalLow\Microsoft\Silverlight\is\vzvpufsm.s4i\m0laonzr.til\1\s\nkhajster01es5wdoyfxd0n5rd2dls3ovyu4wcdig04zjx44hyaaafea\f
When I click on it, it gives me message as "Invalid Image". Can you please tell me how should i write code so that I can see the actual image after saving it in isolated storage ?
private void zipFile()
{
context = Microsoft.SharePoint.Client.ClientContext.Current;
Microsoft.SharePoint.Client.File.OpenBinaryDirect(
context,
#"/TemplateInvoice/" + App.templateFileName + ".xlsx", successFile, FailFile);
}
private void successFile(object sender, OpenBinarySucceededEventArgs args)
{
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
// Obtain the isolated storage for an application.
try
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
Stream strm = args.Stream;
using (var isoStream = store.OpenFile(App.templateFileName + ".zip", FileMode.OpenOrCreate))
{
// Read the resource file into a byte array.
bytes = new byte[strm.Length];
int numBytesToRead = (int)strm.Length;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
// Read may return anything from 0 to numBytesToRead.
int n = strm.Read(bytes, numBytesRead, numBytesToRead);
// The end of the file is reached.
if (n == 0)
break;
numBytesRead += n;
numBytesToRead -= n;
}
numBytesToRead = bytes.Length;
// Write the byte array to the IsolatedStorageFileStream.
isoStream.Write(bytes, 0, numBytesToRead);
//isoStream.Dispose();
}
strm.Close();
string path = App.templateFileName + ".zip";
ZipHelp.UnZip(path, System.IO.Path.GetDirectoryName(path), 4096);
replaceFileContent();
string rootDirectory = System.Windows.Browser.HttpUtility.UrlDecode(path);
string fileName = ZipHelp.Zip(rootDirectory.Replace(".zip", ""), invoice);
//string filename = DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + "Invoice1.xlsx";
// Read the resource file into a byte array.
using (var stream = store.OpenFile(fileName, FileMode.Open))
{
bytes = new byte[stream.Length];
int numBytesToRead = (int)stream.Length;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
// Read may return anything from 0 to numBytesToRead.
int n = stream.Read(bytes, numBytesRead, numBytesToRead);
// The end of the file is reached.
if (n == 0)
break;
numBytesRead += n;
numBytesToRead -= n;
}
InvoiceTemplete invoiceTemplate = new InvoiceTemplete(fileName, bytes, SetMessage);
invoiceTemplate.AddDocument(invoiceTemplate);
}
}
//mark rows as billed
foreach (var item in PrivatePayList)
{
MedwaiverViewModel MedwaiverViewModelObj = new MedwaiverViewModel();
MedwaiverViewModelObj.ChangeBillingStatus(item.ListItemId, "Billed");
if (MedwaiverTimeLogList != null)
{
MedwaiverTimeLogList.Remove(item);
}
if (ClientSpecificTimeLogList != null)
{
ClientSpecificTimeLogList.Remove(item);
}
if (rangeBoundTimeLogListForDate != null)
{
rangeBoundTimeLogListForDate.Remove(item);
}
if (vRangeBoundTimeLogListForDateAndClient != null)
{
vRangeBoundTimeLogListForDateAndClient.Remove(item);
}
}
}
catch (IsolatedStorageException isx)
{
MessageBox.Show(isx.Message);
}
});
}
private void FailFile(object sender, OpenBinaryFailedEventArgs e)
{
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("Fail");
});
}
//The following class zip and unzip the file
class ZipHelp
{
static List<string> folderPathList = new List<string>();
public static string Zip(string rootDirectory, string fileName)
{
byte[] buffer;
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
IsolatedStorageFileStream zipFileStream = store.CreateFile(fileName + ".xlsx");
ZipOutputStream zipOutStream = new ZipOutputStream(zipFileStream);
zipOutStream.UseZip64 = UseZip64.Off;
//zipOutStream.CanPatchEntries
foreach (var item in folderPathList)
{
string entryName = "";
buffer = new byte[4096];
if (item.Substring(0,1) == #"/")
{
//removes leading /
entryName = item.Substring(1);
}
else
{
entryName = item;
}
ZipEntry entry = new ZipEntry(entryName);
//entry.CompressionMethod = CompressionMethod.Deflated;
//entry.
entry.IsZip64Forced();
//entry.IsDirectory
zipOutStream.PutNextEntry(entry);
using (IsolatedStorageFileStream stream = store.OpenFile(rootDirectory + #"\" + item, FileMode.Open))
{
int size;
do
{
size = stream.Read(buffer, 0, buffer.Length);
zipOutStream.Write(buffer, 0, size);
} while (size > 0);
stream.Close();
}
}
zipOutStream.Close();
zipFileStream.Close();
}
return fileName + ".xlsx";
//string[] directories = GetLocationTypes();
//zip();
//string[] filenames = Directory.GetFiles(directories);
//using (var store = IsolatedStorageFile.GetUserStoreForApplication())
//{
// using (var isoStream = store.OpenFile("#" + rootDirectory, FileMode.OpenOrCreate))
// {
// //foreach(string dir in
// }
//}
}
private static string[] GetLocationTypes()
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
return store.GetDirectoryNames();
}
}
/// <summary>
/// UnZip a file
/// </summary>
/// <param name="SrcFile">source file path</param>
/// <param name="DstFile">unzipped file path</param>
/// <param name="BufferSize">buffer to use</param>
public static void UnZip(string SrcFile, string DstFile, int BufferSize)
{
folderPathList.Clear();
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
FileStream fileStreamIn = store.OpenFile(SrcFile, FileMode.Open, FileAccess.Read);
ZipInputStream zipInStream = new ZipInputStream(fileStreamIn);
string rootDirectory = System.Windows.Browser.HttpUtility.UrlDecode(SrcFile);
rootDirectory = rootDirectory.Replace(".zip", "");
store.CreateDirectory(rootDirectory);
while (true)
{
ZipEntry entry = zipInStream.GetNextEntry();
if (entry == null)
break;
if (entry.Name.Contains("/"))
{
string[] folders = entry.Name.Split('/');
string lastElement = folders[folders.Length - 1];
var folderList = new List<string>(folders);
folderList.RemoveAt(folders.Length - 1);
folders = folderList.ToArray();
string folderPath = "";
foreach (string str in folders)
{
folderPath = folderPath + "/" + str;
if (!store.DirectoryExists(rootDirectory + "/" + folderPath))
{
store.CreateDirectory(rootDirectory + "/" + folderPath);
}
}
folderPath = folderPath + "/" + lastElement;
writeToFile(BufferSize, fileStreamIn, zipInStream, rootDirectory, folderPath);
}
else
{
writeToFile(BufferSize, fileStreamIn, zipInStream, rootDirectory, entry.Name);
}
}
zipInStream.Close();
fileStreamIn.Close();
}
private static void writeToFile(int BufferSize, FileStream fileStreamIn, ZipInputStream zipInStream, string rootDirectory, string folderPath)
{
IsolatedStorageFile store1 = IsolatedStorageFile.GetUserStoreForApplication();
FileStream fileStreamOut = store1.OpenFile(rootDirectory + "/" + folderPath, FileMode.Create, FileAccess.Write);
folderPathList.Add(folderPath);
int size;
byte[] buffer = new byte[BufferSize];
do
{
size = zipInStream.Read(buffer, 0, buffer.Length);
fileStreamOut.Write(buffer, 0, size);
} while (size > 0);
fileStreamOut.Close();
}
}
I can think of two viable options here.
In your code above, save off the Pixels array of the WriteableBitmap. Then to restore it, you would create a WriteableBitmap of the appropriate size and the set the Pixels array to the stored data.
-or-
Use a HttpWebRequest or WebClient request to get the raw image stream and save that to IsolatedStorage.
There are pros and cons to each of these, in the first case the data is uncompressed and will take up more space in isolated storage and you would not be able to open the image from disk outside of Silverlight, similar to the issue you had above. For the second option you can run into cross domain issues if the images are on a different server than your Silverlight XAP file and it's also a bit more complex to implement.
The basic problem is that you're storing the decoded RGBA pixels array as a JPEG file - but they're not the same. Your code is basically correct: you just can't use this method to store it as a JPEG file. But if you happen to know that the image is, say, 800x600, you could create an 800x600 WriteableBitmap, and then set the Pixels property to the retrieved stream's byte array. Or if you don't know the dimensions when you're retrieving it, you could store the dimensions as the first two integers in the stream (before you write the pixels to the stream), and read out the dimensions when you're reading the file. You'd effectively be creating your own very simplistic .bmp format.

Resources