I downloaded a image by using this method .
public Image getImageFromURL(String sURL)
{
using (WebClient wc = new WebClient())
{
var data = wc.DownloadData(sURL);
var image = Image.FromStream(new MemoryStream(data));
return image;
}
}
In my razor view and it returns me Bitmap object , but when i want to show the downloaded in image then it doesn't work
Thanks in advance
If you get Image in response from your getImageFromURL(String sURL) method.
you need to store image at any location in your application, then you can render it on view.
you can use this code.
var image = Image.FromStream(new MemoryStream(data));
string path=Path.Combine(Server.MapPath("~/images"), imagename);
image.Save(path);
this will save image at your images folder, and then you can render your image from this images folder location on view.
<img src="~/images/imagename" alt="" />
Related
I need to develop a web application which needs to scan the QR code and read the contents of it.
Is it possible with Asp.net Web application , the code behind used is C#.
Is it possible to take a picture and save it using c#.net in web application
Take a picture in the browser client. Send image data to c# pages.
<input id="upload" name="upload" type="file" accept="image/*" />
It is just a html file input,you can get the QR image data,when image received by select or camera .
$("#upload").on('change', function() {
var file = $(this)[0].files[0];
if(!file) {//undefined
return;
}
if(!startLoading()) {
return;
}
var file = $(this)[0].files[0];
var reader = new FileReader();
reader.readAsDataURL(file); // read file as Data URL
reader.onload = function() {
var base64 = this.result;
//send this base64 string to c# backend page using ajax
...
});
Then code in you c# page,get the base64 string,change to image.
byte[] arr2 = Convert.FromBase64String(base64);
using (MemoryStream ms2 = new MemoryStream(arr2))
{
System.Drawing.Bitmap bmp2 = new System.Drawing.Bitmap(ms2);
bmp2.Save(filePath + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
...
i am woring in Asp.net(C#) .i am trying convert html to PDF using Pechkin, PDF convert successfully.., but in my html tag is their that image is not showing in PDF,please Help Me
My html code is
<img id="ContentPlaceHolder1_img_header"
src="/CompanyLogos/12_09_YYYY_07_41_48_logo.png" style="width:130px"
and my .cs page Code IS
byte[] pdfContent = new SynchronizedPechkin(
new GlobalConfig())
.Convert(
new ObjectConfig()
.SetLoadImages(true)
.SetPrintBackground(true)
.SetCreateExternalLinks(true)
.SetAllowLocalContent(true),
html);
Msg.Attachments.Add(new Attachment(new MemoryStream(pdfContent), filename.ToString()));
As for your image, you'll need to convert it to base64, VERY rough example
var base64 = convertTobase64(myImg);
<img src=base64 />
An example of the converting an image to base64 in Asp.NET can be found here. After converting the image to a base64 string, set the source of your image to the base64 string instead of the image URL. Then your image will appear in your PDF.
If you wanna accomplish this in javascript/html it's pretty easy.
<canvas id='imgCanvas' style='display:none;'></canvas>
var dataUrl;
var myImg = new Image();
var canvas = document.getElementById('imgCanvas');
$(myImg).on('load', function(){
canvas.height = img.height;
canvas.width = img.width;
var context = canvas.getContext('2d');
context.drawImage(img, 0, 0);
dataUrl = canvas.toDataUrl('png');
});
img.src = '/CompanyLogos/12_09_YYYY_07_41_48_logo.png'
And then you just set the source of your image tag to the dataUrl variable and it will render in your PDF.
I am trying to download image from <img src="...">.
If the src contains the file name and extension, I can download image simply passing url to my program.
However, when the src does not contains file name and extension, like
<img style="-webkit-user-select: none; cursor: zoom-in;" src="https://secure.somewebsite.net/website/member.php/1/folders/get_attach?mail_id=11111&attach_id=1">
it is possible to download the image from C# code?
Thank you.
Here is a complete sample
using (WebClient client = new WebClient())
{
client.DownloadFile("http://somedomain/image.png",
#"c:\temp\image.png");
// You can also you the async call
client.DownloadFileAsync("http://somedomain/image.png",
#"c:\temp\image.png");
}
Yes it is possible,
You can use these methods to download the image from c# :
client.DownloadFileAsync(new Uri(url), #"c:\temp\img.png");
client.DownloadFile(new Uri(url), #"c:\temp\img.png");
Now, WebClient is obselete (-> https://learn.microsoft.com/en-us/dotnet/fundamentals/syslib-diagnostics/syslib0014)
use for exemple
using var httpClient = new HttpClient();
var streamGot = await httpClient.GetStreamAsync("http://somedomain/image.png");
await using var fileStream = new FileStream("img.png", FileMode.Create, FileAccess.Write);
streamGot.CopyTo(fileStream);
I have a page http://www.mysite.com/image.aspx, that I want to load and display an image instead of rendering HTML.
I have the ContentType of the page set to image/png, and here's my code:
using (Bitmap image = new Bitmap("http://www.google.com/images/img.png"))
{
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.WriteTo(Response.OutputStream);
}
}
But I get an error saying:
URI formats are not supported.
How can I load an external image and render it to the page?
You can't load a Bitmap using a URI - it has to be a local file to your computer.
If you want to load an image from off the web and then render it, you need to make a web request off to that specific resource and then render the bytes to the stream as you are doing.
AKA
WebRequest webRequest = WebRequest.Create("http://www.google.com/images/img.png");
using(WebResponse response = webRequest.GetResponse())
{
using(MemoryStream stream = new MemoryStream(response.GetResponseStream())
{
stream.WriteTo(Response.OutputStream);
}
}
I know how to save an image to a folder using the fileupload control with the saveas method. But I to take an image from the image control and save it to a file without using the fileupload control n save it in folder.
string filepath = img1.ImageUrl;
using (WebClient client = new WebClient())
{
client.DownloadFile(filepath,Server.MapPath("~/Image/apple.jpg"));
}
Do you know image path? you can get image path from image control and then download image in code:
Download image from the site in .NET/C#
using(WebClient client = new WebClient())
{
client.DownloadFile("http://www.example.com/image.jpg", localFilename);
}
First Get the Url of Image and then using webclient you can save file in folder
string filepath = img1.ImageUrl;
using (WebClient client = new WebClient())
{
client.DownloadFile(filepath,Server.MapPath("~/Image/apple.jpg"));
}
This will save image in Image Folder with ImageName apple...