I wanted to export a report with image. The stored image is in URL format in DB.
My alternative is download the file physically and pass the file path in to show the image, but this way seem redundant. Sample from here
What would be the best way to do this? Many thanks!
I end up using the code below to download the image and convert it into base64 before I display in RDLC.
var webClient = new WebClient();
byte[] imageBytes = webClient.DownloadData(urlimg);
DataRow drow = table.NewRow();
drow["filepath"] = Convert.ToBase64String(imageBytes);
RDLC are configured as image below.
Related
How to upload a Image with RestSharp without using a local path.
Image exists only in a Image Variable.
All i found was with a string as path, but this is not the way i want to go.
Or is it possible to get the string Path from a ImageSource?
How can this be solved?
My current code:
var client = new RestClient();
var request = new RestRequest(PostImageUrl, Method.Post);
request.AddOrUpdateHeader("Content-Type", "multipart/form-data");
request.AddFile("image", bauzeichnung);
request.AlwaysMultipartFormData = true;
var response = client.Execute(request);
It is not possible, every file is need a source. But I can suggest methods that I do not recommend.
You can convert image to base64 string and save image as string
You can take Url of image and show in your app. But in this case image is should be updated to network before(Url).
You can ConvertImage to base64 and store it in your cache.
You can convert Image to ByteArray.
None of these recommendations are correct methods!!!
The most accurate method;
You can take the image and store it in your machine(Local or Server). Then tahe the path of image and save database as Url(If you don't know the difference between url and path, you should research it). When you send image just send the url of your image.
I have a large number of images on a Web server that need to be cropped. I would like to automate this process.
So my thought is to create a routine that, given the URL of the image, downloads the image, crops it, then uploads it back to the server (as a different file). I don't want to save the image locally, and I don't want to display the image to the screen.
I already have a project in C#.Net that I'd like to do this in, but I could do .Net Core if I have to.
I have looked around, but all the information I could find for downloading an image involves saving the file locally, and all the information I could find about cropping involves displaying the image to the screen.
Is there a way to do what I need?
It's perfectly possible to issue a GET request to a URL and have the response returned to you as a byte[] using HttpClient.GetByteArrayAsync. With that binary content, you can read it into an Image using Image.FromStream.
Once you have that Image object, you can use the answer from here to do your cropping.
//Note: You only want a single HttpClient in your application
//and re-use it where possible to avoid socket exhaustion issues
using (var httpClient = new HttpClient())
{
//Issue the GET request to a URL and read the response into a
//stream that can be used to load the image
var imageContent = await httpClient.GetByteArrayAsync("<your image url>");
using (var imageBuffer = new MemoryStream(imageContent))
{
var image = Image.FromStream(imageBuffer);
//Do something with image
}
}
I'm new to stack overflow, C# and onenote interop com api. I'm trying to display a pdf file in onenote using C# and the onenote com/interop api (I'd rather not use the REST API).
I am able to display a link to a pdf file using the tag < InsertedFile pathSource="[myfilepath]" preferredName = "[myPreferredName]"> in conjunction with the UpdatePageContent function in the interop API, but this doesn't display the PDF.
I have been able to get my program to display an image in onenote using the following code to create the image tag
private XElement createImageTag(Image image)
{
string OneNoteNamespace = "http://schemas.microsoft.com/office/onenote/2013/onenote";
var img = new XElement(XName.Get("Image", OneNoteNamespace));
var data = new XElement(XName.Get("Data", OneNoteNamespace));
data.Value = this.toBase64(image);
img.Add(data);
return img;
}
private string toBase64(Image image)
{
using (var memoryStream = new MemoryStream())
{
image.Save(memoryStream, ImageFormat.Png);
var binary = memoryStream.ToArray();
return Convert.ToBase64String(binary);
}
}
I tried altering this for a pdf instead of am image by converting a pdf to a byte array then converting it to base64 and assigning the result as data.Value in the createImageTag function but it did not result in a displayed pdf either (presumably because onenote was expecting an image and not a pdf). I'd like to avoid using third party libraries or extensions to convert a pdf to an image if possible, and haven't found any other ways to convert a pdf to an image.
I used ONOMSpy to look for any other onenote/xml tags that might help me display a pdf in onenote, but didn't see others besides the Image and InsertedFile tags that looked like they were close to doing what I wanted.
so if you could help me either :
1) find an easy way to convert a pdf to an image using C# or
2) show me how to tell onenote to display the PDF
I'd really appreciate it. Thanks!
I would like to display images in excel file which is generated out of my c# code. Currently I am generating the xls file using the following code:
StringBuilder sb = new StringBuilder();
string imageInitialPath = Request.UrlReferrer.ToString().Substring(0, Request.UrlReferrer.ToString().LastIndexOf('/'));
sb.Append("<table border=1><tr><td colspan=3 rowspan=5 style=text-align: left;><img src='" + imageInitialPath + "/images/abc.JPG'/></td></tr></table>");
Here I am giving the path of an image from some folder. The image is gone from excel when deleted from that folder. Is there a way to keep the image in excel without actually depending on source?
Thanks
The image in the resulting excel file may still be referenced via the link, since the source is html.
I would suggest creating an actual Excel file format using this free library: http://epplus.codeplex.com/
I want to export one gridview table to excel format.
The most simple and fast forward solution that I found is from Math Berseth
http://mattberseth.com/blog/2007/04/export_gridview_to_excel_1.html
This solution works fine and was accepted by client. But now, after some months, a new feature was requested: "Just put one image logo in excel"
This is freak me out. I can't put the System.Drawing.Image in a System.Web.UI.WebControls.Image cause they are completely different, but I'm not able to just put a Path cause the excel generated will be send in e-mails so Directory structure can't be considered.
So, can I put images retrieved from bytes in Gridview to export in Math model, or exist some other way?
edit..
I walk few more steps but I'm still far away from my goal.
I can embedded images in html files using String Base64
Something like:
private string MakeImageSrcData(string filename) {
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
byte[] filebytes = new byte[fs.Length];
fs.Read(filebytes, 0, Convert.ToInt32(fs.Length));
return "data:image/png;base64," + Convert.ToBase64String(filebytes, Base64FormattingOptions.None);
}
...
string base64 = MakeImageSrcData("D:\\Proj\\top_title.png");
TableRow tr = new TableRow();
TableCell tc = new TableCell();
Image logoEmpresa = new Image();
logoEmpresa.ImageUrl = base64;
tc.Controls.Add(logoEmpresa);
tr.Cells.Add(tc);
table.Rows.Add(tr);
This works fine with IE and FF but nothing whit excel :/
I tried spreadsheet xml, but as MSDN describes here there are no support to image type.
Some other idea?
This method works by outputting the gridview as text containing an HTML table. I would imagine you could prepend the string with a string containing an <img> tag pointing to your logo somewhere on an accessible web site.
Give it a try.
Comment
I added the following before line 61 in the GridViewExportUtil.cs file in the referenced demo:
HttpContext.Current.Response.Write("<img src='http://localhost/WebApplication2/wand.gif' />");
The image was available at the specified URL, and rendered correctly in Excel.