Simple question: how to display retrieved ravendb attachment image in winforms pictureBox.
Attachment is retrieved as
Raven.Abstractions.Data.Attachment attachment =
_store.DatabaseCommands.GetAttachment("upload/"+ 9999);
update
Image is save with put attachment like this
_Store.DatabaseCommands.PutAttachment("upload/" + attachId, null, ms,
new RavenJObject
{
{ "Content-Type", "image/jpeg" }
});
ms is memory stream
You need to retrieve the attachment memory stream from the Attachment object:
pictureBox1.Image = Image.FromStream(attachment.Data());
See more in the docs
Related
I am trying to store an uploaded file (via ajax file upload) into a LONGBLOB column of a MySQL database. Reason for a LONGBLOB, the user is allowed to upload a file up until 10MB in file size.
The first issue is that I couldn't upload just the file byte[] into the column, as it complained about an "incorrect string value".
So, I decided to do a Convert.ToBase64String of the file byte[] and store that. No problem (I hope).
My main problem is that I want to retrieve that stored blob and write it to an Outputstream in order to view the file in the browser. The file is however corrupt.
The difference that I do pick up is that the byte[] length of the original file is significantly less than the byte[] of the blob (after I retrieve and convert it back to a byte[]).
So, code wise, here is what I have attempted for the saving of the uploaded file:
byte[] fileData = null;
//file is the uploaded file
file.InputStream.Position = 0;
using (var reader = new BinaryReader(file.InputStream))
{
fileData = reader.ReadBytes(file.ContentLength);
}
var encryptedFileStream = Convert.ToBase64String(fileData);
//Storing encryptedFileStream to LONGBLOB type
Below is what I have attempted for the reading of the blob into a OutputStream:
var decryptedFileStream = Convert.FromBase64String(Convert.ToBase64String(file.FileStream)); //file.FileStream is a byte[] datatype.
WebClient client = new WebClient();
var response = ((Handler)ExecutionContext).Response;
response.Clear();
response.ContentType = file.FileExtension;
response.AppendHeader("content-disposition", (inline ? "inline" : "attachment") + "; filename=" + file.FileName);
response.AppendHeader("content-length", decryptedFileStream.Length.ToString());
response.OutputStream.Write(decryptedFileStream, 0, decryptedFileStream.Length);
I have looked every where for my answer but couldn't find the right solution.Tried many solutions provided but still can't get it through.I uploaded an image in ftp server and i want it to get displayed into picture box in windows form without downloading it into local machine. Is it possible?
Please include complete code for the solution......
Here is a complete code: If any body needs.Make sure the image isn't large!!
public byte [] GetImgByte (string ftpFilePath)
{
WebClient ftpClient = new WebClient();
ftpClient.Credentials = new NetworkCredential(ftpUsername,ftpPassword);
byte[] imageByte = ftpClient.DownloadData(ftpFilePath);
return imageByte;
}
public static Bitmap ByteToImage(byte[] blob)
{
MemoryStream mStream = new MemoryStream();
byte[] pData = blob;
mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
Bitmap bm = new Bitmap(mStream, false);
mStream.Dispose();
return bm;
}
You can use DownloadData to get a byte array and load that into the picturebox - see Download file directly to memory and How to put image in a picture box from a byte[] in C#
I'm using PDFSharp and I'm creating pdf's from a bitmap image. I can save and view the image as a pdf no problem. But now what I want to do is before the pdf is saved as an actual file, I want to convert it to byte[]. I can save it as a byte[] after the fact I save it. I'm going to have two different methods, one that saves the pdf to a file where the user can then open it, and another that saves the pdf to byte that will be sent to a databse, but I may not do both. I may need to save a file one day and another save it as a byte without needing to save it as a file. WhatI have:
public void DrawImage(Bitmap thumbnail)//thumbnail is created with another method
{
PdfDocument document = new PdfDocument();
document.Info.Title = "Created with PDFsharp";
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
XImage image = XImage.FromGdiPlusImage(thumbnail);
gfx.DrawImage(image, 0, 0, 612, 792);
string filename = string.Format(#"{0}.pdf", Guid.NewGuid());
//This is Save(string path), there is also a Save(memoryStream stream)
document.Save(filename);
//so before I save it as a *.PDF I would like to save it as byte[] that can be sent to a Database, and eventually read from and convert the byte to a viewable *.pdf
//This saves a Bitmap as a pdf successfully
}
I hope I'm making sense, I can explain further if needed
You can use MemoryStream for this purpose:
byte[] pdfData;
using (var ms = new MemoryStream()) {
document.Save(ms);
pdfData = ms.ToArray();
}
Try using a MemoryStream:
using(var ms = new MemoryStream()){
document.Save(ms);
byte[] bytes = ms.ToArray();
}
Is it possible to attach a document to an email without saving it on the server?
The code below is something that attaches only after saving to the server. What I am looking for is to attach the document to the email without having it saved first instead just attach to the email from the path provided.
This is in Visual Studio 2005 using c#
if (SaveDocument.HasFile)
{
/* Get a reference to PostedFile object */
string strFileName = Path.GetFileName(SaveDocument.PostedFile.FileName);
/* Save the file on the server */
SaveDocument.PostedFile.SaveAs(Server.MapPath(strFileName));
/* Create the email attachment with the uploaded file */
System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(Server.MapPath(strFileName));
/* Attach the newly created email attachment */
message.Attachments.Add(attach);
}
Sure, there's a constructor overload for Attachment which accepts a Stream instead of a file name. So, for example, if you have a byte[] of data, you can create an Attachment from it:
var contentType new ContentType(MediaTypeNames.Text.Plain);
var attach = new Attachment(new MemoryStream(data), contentType);
Yes, just use the constructor that takes a stream rather than a filename.
See:
http://msdn.microsoft.com/en-us/library/ab7hb4y5(v=vs.110).aspx
Pass the PostedFile's InputStream directly to the Attachment.
if (SaveDocument.HasFile)
{
/* Create the email attachment with the uploaded file */
System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(SaveDocument.PostedFile.InputStream, "filename");
/* Attach the newly created email attachment */
message.Attachments.Add(attach);
}
I haven't tried it yet, but it should work.
Up to know i can load the attachments to memory and i know its right cause i can print the name of the file. What i need is to convert this attachment to an image object which i will later add to a sharepoint picture library. But forget about the sharepoint part i know how to do that, am stuck in the part that after loading the attachments how do i conver this into images. I dont want to save the images in disk cause thats not the point i already load them in memory.
foreach (Item item in findResults.Items)
{
if (item is EmailMessage && item.HasAttachments)
{
EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));
foreach (Attachment attachment in message.Attachments)
{
if (attachment is FileAttachment)
{
FileAttachment fileAttachment = attachment as FileAttachment;
// Load the file attachment into memory and print out its file name.
fileAttachment.Load();
Console.WriteLine("Attachment name: " + fileAttachment.Name);
//this is where i would create the image of object but dont know how
}
}
}
}
You already have the FileAttachment object, and you even access one of its properties. You only need to take the next step, and access not only the Name but also the Content.
if (attachment is FileAttachment)
{
FileAttachment fileAttachment = attachment as FileAttachment;
fileAttachment.Load();
byte[] fileContent = fileAttachment.Content;
}
This will give you the contents on the attachemnts, as an array of bytes. I don't remember what the Sharepoint API wants to receive, but it's either this byte array or something you can easily build out of it.