Accessing image from other server - c#

I have image file placed on the one server and application on other server.
I want to access that image, below code I have written:
On default.aspx, I have
<asp:Image ID="Image1" runat="server" ImageUrl= "GetImage.aspx?imgName=MyImage.jpg" />
and on GetImage.aspx, I have written the below code on page_load
protected void Page_Load(object sender, EventArgs e)
{
// Changing the page's content type to indicate the page is returning an image
Response.ContentType = "image/jpg";
var imageName = Request.QueryString["imgName"];
var path = "//SERVER/FOLDER/" + imageName;
if ((string.IsNullOrEmpty(imageName) == false))
{
// Retrieving the image
System.Drawing.Image fullSizeImg;
fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(path));
// Writing the image directly to the output stream
fullSizeImg.Save(Response.OutputStream, ImageFormat.Jpeg);
// Cleaning up the image
fullSizeImg.Dispose();
}
}
But I am getting error at
fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(path));
Please let me know where I am incorrect. Do I need to anything else other than Server.MapPath ? because my image is on other server.
EDIT
I have image folder in my computer
I have created a web app in other computer [same network], deployed
in IIS, image is displayed correctly. With path like
http://10.67.XX.XX/websiteName/Default.aspx
but when I am trying to access the same from my comupter or any other
computer, I am not able to see the image.

You shouldn't use Server.MapPath. This is used to map virtual paths under your site to physical paths under file system. If the file exists on another server, just access it by name directly, without Server.MapPath.

The answer above is incorrect because the images reside on a separate server.
So System.Images will not no where the image is.
Secondly you have to use server.Mappath for system images, it requires a windows path i.e. c:\blah\blah\whatever.jpg.
you will need to use \\server\C$\folder\image.jpg this works well with system.image.FromFile
Also saving you can also utilize this.
Cheers

Related

ASPNET image can't show in chrome

I have the remote computer, it name is A11
and a image file name is A22
i try my code use aspnet run IE11 , IE11 can show image ,but chrome can't show.
my code is same , as follow
protected void Page_Load(object sender, EventArgs e)
{
Image2.ImageUrl = #"file://A11/A2222.jpg";
Image1.ImageUrl = #"\\A11\\A2222.jpg";
}
i try other method
FileInfo fi = new FileInfo(#"\\A11\\A2222.jpg");
Response.AddHeader("Content-Disposition", "inline;Filename=" + #"\\A11\\A2222.jpg");
Response.AddHeader("Content-Length", fi.ToString());
other method:
clear Cache
open image display option
but it is not working for chrome, it still no show my image
how can i do ?
ps.
chrome ver.78.0.3904.108
Most modern browsers doesn't allow local images to be shown unless you open a static html file from local disk. Since you are using asp.net webforms I will assume you are not doing this and you might need to rethink your approach.
If you simply need to be able to use images from a fileshare I would instead make a virtual directory on IIS and use this instead, so links becomes /virtualdirectory/somefile.jpg for example.

Image is not shown on live server

I have a logo that I need to show inside a mail. On my local machine, the logo is shown, but on the server where the live environment is, the logo is not shown.
I'm using the path like this:
imagePath = "~/Images/email-logo2.png";
The email-logo2.png exists in the same folder on the local machine and on the server, too.
I have tried to add permissions to read in the server folder where the png exists, but it did not resolve the problem. Can you advise?
The image is added in the email like this:
HTML:
<img class="auto-style4" src="{PictureSrc}" /><br />
C# code:
switch (property)
{
case "PictureSrc":
string imagePath = "";
if (User.Identity.GetUserId<int>() == 3140 ||
User.Identity.GetUserId<int>() == 3142)
{
imagePath = "~/Images/email-logo2.png";
}
else
{
imagePath = "~/Images/email-logo.png";
}
content = content.Replace("{" + property + "}", HttpContext.Server.MapPath(imagePath));
Server.MapPath returns a path on disk (e.g. C:\images\image.png), not a URL. See https://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath(v=vs.110).aspx.
So a user viewing the email from another machine will obviously not be able to resolve a path on the server's disk, it has no access to that disk.
The image path you provide has to be a fully qualified URL e.g. http://www.example.com/images/image.png.
It worked locally because you're on the same machine as where the image is located, so it happens to have access to that path, but that's not true for everyone else using it.
Alternatively, if the image is not too large you can convert it to base64 and embed it into the HTML in the email.

Saving a file in a WCF service to Images folder

So here is the problem. i am saving images in binary form to db. In the WCF service i created I want to save the file to a folder within the project i.e (/Images).
I normally use this code
//picbin is binary image data fetched from db
ImageConverter ic = new ImageConverter();
System.Drawing.Image img = (System.Drawing.Image)ic.ConvertFrom(picbin);
img.Save(HttpContext.Current.Server.MapPath(imagePath + picture_id + ".Jpeg"), System.Drawing.Imaging.ImageFormat.Jpeg);// imagePath ="Images/"
The problem is HttpContext.Current.Server.MapPath does not work in WCF services. so how can I get the physical path to save the image. Also what Url can i Get to access the image
I tried Using HostingEnvironment.MapPath, but the path it returns is always null.
You can use HostingEnvironment.MapPath.
please check below link
https://stackoverflow.com/a/10384987/2699211
img.Save("website absolute path/" +(imagePath + picture_id + ".Jpeg"),
you need to provide the absolute path. it works on my side.

Creating Image object using System.Drawing.Image.FromFile

I am trying to get the image dimensions of an image that user selects from list box. Image files are available on FTP server. I am displaying file names in a list box for users to select. Upon selection, I want to show the preview of image, for that I want to get dimensions so that I can resize it if i need to.
I am storing file name that is linked to currently selected list item into a string variable. I know that path on the server. I am using following code to create the Image object, but having no luck
try
{
string dir = Session["currentUser"].ToString();
System.Drawing.Image img = System.Drawing.Image.FromFile("~/Uploads/"+dir+"/"+fName, true); //ERROR here, it gives me file URL as error message!
}
catch(Exception ex)
{
lbl_Err.Text = ex.Message;
}
Not sure what is going wrong. Any ideas?
use Server.MapPath to fetch the image from the server.
As follows
System.Drawing.Image img =
System.Drawing.Image.FromFile(Server.MapPath("Uploads/"+dir+"/"+fName), true);
You can use following as well
Server.MapPath(".") returns the current physical directory of the file (e.g. aspx) being executed
Server.MapPath("..") returns the parent directory
Server.MapPath("~") returns the physical path to the root of the application
Server.MapPath("/") returns the physical path to the root of the domain name (is not necessarily the same as the root of the application)
References
Server.MapPath("."), Server.MapPath("~"), Server.MapPath(#"\"), Server.MapPath("/"). What is the difference?

C# - Get Image path from Client Machine

I want to get the path of the Image which is saved in Client Machine. I know the Path and the file name of the Image. By using FileUpload i can do it, but without using fileupload is it possible to get the path of the file ??.
My Scenario is given below,
Public void ConverttoByte()
{
//Get the image path from web.config & this image is in client machine
string strConfig = #"C:\Manikandan\image\image1.jpg";
MemoryStream MS = new MemoryStream();
Byte[] data;
int fiFileSize;
System.Drawing.Image image;
image = System.Drawing.Image.FromFile(strConfig);
image.Save(MS, System.Drawing.Imaging.ImageFormat.Gif);
data = MS.ToArray();
CallDBMethod(data);
}
Here I converted the image as Byte and I called the CallDBMethod to insert this byte details to DB..
This image is available in client machine, but not in server machine..
So, how to i get this image path from client machine & how can i solve this?
Without the File upload control, It is not possible unless you create an Activex control (may not work in all browsers. User has to give permission to run this control).
In a web application, You can not take ( steal) any file from the user's computer without them making an action to do so( ex : selecting the file in the File Input control and clicking some upload button).
If you want the full file path of the file user selected in the file upload control, you can get by HttpPostedFile.FileName property which gives you the fully qualified name of the file on the client (Ex : C:\MySomeFolder\SomeFile.jpg).
string fullPath=FileUpload1.HttpPostedFile.FileName
Assuming FileUpload1 is the ID of the File Upload control.
According to my knowledge it is not possible. You can't get the client's image path. The browser does not allow us to get the path. Using fileupload control you can also not get the complete path.
A similar question by me
How to Get complete file path using file upload control in asp.net or any other way?

Categories