How can I open a file in wp7? - windows-phone-7.1

I have some files in IsolatedStorage in my application. The file types are different, say doc,xls,ppt,pdf,mp3,mov,jpg,png etc.. I need to open these files. How can I do this.

Try to open it with name and its extensions
byte[] data;
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isfs = isf.OpenFile(image.jpg, FileMode.Open, FileAccess.Read))
{
data = new byte[isfs.Length];
isfs.Read(data, 0, data.Length);
isfs.Close();
}
}
MemoryStream ms = new MemoryStream(data);
BitmapImage bi = new BitmapImage();
bi.SetSource(ms);
Image img = new image();
img.source = bi
if it is an image try to set the source of a bitmap image as memory stream ms.

Related

How to get Image Name or Id from docx file using Open xml in C#?

I am using Mvc.Net API with open XML. I need to replace multiple images in .docx file.I replace the images in current scenario but I don't get any Id or Name of the Image at my code side so facing difficulties to replace those images.
Here is my code
List<ImagePart> imgPartList = doc.MainDocumentPart.ImageParts.ToList();
foreach(ImagePart imgPart in imgPartList)
{
string Id=doc.MainDocumentPart.GetIdOfPart(imgPart);
byte[] imageBytes = File.ReadAllBytes(ImagePath);
BinaryWriter writer = new BinaryWriter(imgPart.GetStream());
writer.Write(imageBytes);
writer.Close();
}
Can I get the name of Image in ImagePart?
I would do something like this:
List<ImagePart> imgPartList = doc.MainDocumentPart.ImageParts.ToList();
foreach(ImagePart imgPart in imgPartList)
{
var imageId = document.MainDocumentPart.GetIdOfPart(imgPart.Id);
byte[] imageBytes = File.ReadAllBytes(ImagePath);
BinaryWriter writer = new BinaryWriter(imgPart.GetStream());
writer.Write(imageBytes);
writer.Close();
}
Also
This answer could help you

How to save and load a PDF into memory stream using Spire PDF C#

public MemoryStream PdfGeneration(Model model)
{
string path = HttpContext.Current.Server.MapPath("test.pdf");
path = path.Replace("\\api", "");
FileStream fs = new FileStream(path, FileMode.Create);
doc.SaveToStream(fs);
MemoryStream ms = new MemoryStream();
ms.SetLength(fs.Length);
fs.Read(ms.GetBuffer(), 0, (int)fs.Length);
ms.Flush();
fs.Close();
return ms;
}
PS: I don't want the file to be read from the disk, it has to be processed in memory.
So here I am using Spire PDF as a generator and I need to save it to memory stream and send as an attachment to mail.
To load file from stream, you can use LoadFromStream method
PdfDocument doc = new PdfDocument();
doc.LoadFromStream(Stream stream);

PDFSharp download file thru filestream

I am new to this library but I couldn't find anything about downloading to the filestream with this library, I could only find a document.Save(filepath) option which is not always allowed.
I would like to create a filestream so the file gets downloaded to the directed Downloads folder.
Could someone point me in the right direct?
There is a Save overload that takes a MemoryStream object instead of path to a file.
The PDFSharp website has an example showing how to do this:
private void Page_Load(object sender, System.EventArgs e)
{
// Create new PDF document
PdfDocument document = new PdfDocument();
this.time = document.Info.CreationDate;
document.Info.Title = "PDFsharp Clock Demo";
document.Info.Author = "Stefan Lange";
document.Info.Subject = "Server time: " +
this.time.ToString("F", CultureInfo.InvariantCulture);
// Create new page
PdfPage page = document.AddPage();
page.Width = XUnit.FromMillimeter(200);
page.Height = XUnit.FromMillimeter(200);
// Create graphics object and draw clock
XGraphics gfx = XGraphics.FromPdfPage(page);
RenderClock(gfx);
// Send PDF to browser
MemoryStream stream = new MemoryStream();
document.Save(stream, false);
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", stream.Length.ToString());
Response.BinaryWrite(stream.ToArray());
Response.Flush();
stream.Close();
Response.End();
}

When upload Bitmap stream to Azure storage, it store zero/empty image

I am using below code to upload a MemoryStream from a bitmap image to my Microsoft azure storage account:
MemoryStream memoryStream = new MemoryStream();
img.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
blob.Properties.ContentType = model.File.ContentType;
blob.UploadFromStream(memoryStream);
What happen by use above code is it uploads an empty image to Azure storage :( !(I found the name but the file size is zero!)!
if (model.File != null && model.File.ContentLength > 0)
{
Bitmap original = null;
var name = "newimagefile";
var errorField = string.Empty;
errorField = "File";
name = Path.GetFileNameWithoutExtension(model.File.FileName);
original = Bitmap.FromStream(model.File.InputStream) as Bitmap;
if (original != null)
{
var img = CreateImage(original, model.X, model.Y, model.Width, model.Height);
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("company"); // must always be lowercase
container.CreateIfNotExists();
container.SetPermissions(
new BlobContainerPermissions
{
PublicAccess =
BlobContainerPublicAccessType.Blob
});
CloudBlockBlob blob = container.GetBlockBlobReference(imgName + ".png");
MemoryStream memoryStream = new MemoryStream();
img.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
blob.Properties.ContentType = model.File.ContentType;
blob.UploadFromStream(memoryStream);
}
Any Help please !
I think you need to set the position back to the start of the stream before uploading
img.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
blob.Properties.ContentType = model.File.ContentType;
memoryStream.Position = 0;
blob.UploadFromStream(memoryStream);

Adding to WordprocessingDocument opened from MemoryStream without getting "Memory stream is not expandable"?

Using Open XML SDK, the following gives "Memory stream is not expandable" when I reach the line FeedData(msData):
// Bytes in, bytes out
internal static byte[] UpdateDataStoreInMemoryStream(byte[] bytes,
XmlDocument xdocData)
{
using (var msDoc = new MemoryStream(bytes))
{
using (WordprocessingDocument wd = WordprocessingDocument.Open(msDoc, true))
{
MainDocumentPart mdp = wd.MainDocumentPart;
CustomXmlPart cxp = mdp.CustomXmlParts.SingleOrDefault<CustomXmlPart>();
using (MemoryStream msData = new MemoryStream())
{
xdocData.Save(msData);
msData.Position = 0;
// Replace content of ...\customXml\item1.xml.
cxp.FeedData(msData);
// "Memory stream is not expandable" if more data than was there initially.
}
}
return msDoc.ToArray();
}
}
Note: it is not msData that is the trouble but msDoc.
Stein-Tore
The trouble was (actually quite obvious from the error message) that
using (var msDoc = new MemoryStream(bytes)) ...
creates a fixed size MemoryStream.
So solution is to create an expandable MemoryStream:
MemoryStream msDoc = new MemoryStream();
msDoc.Write(bytes, 0, bytes.Length);
msDoc.Position = 0;
...

Resources