Saving images on the server - c#

I'm getting the "generic error occurred on GDI++ wen trying to save images.
What I am missing here?
string appdatapath = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
if (!Directory.Exists(appdatapath + "/Images")
Directory.CreateDirectory(appdatapath + "/Images", Directory.GetAccessControl(appdatapath));
if (!Directory.Exists(appdatapath + "/Images/XBLContent/"))
Directory.CreateDirectory(appdatapath + "/Images/XBLContent/", Directory.GetAccessControl(appdatapath));
string imagesdir = Path.Combine(appdatapath, "/Images/XBLContent/");
Image image = FeedUtils.RequestImage(String.Format({0}/{1}/image.jpg", url, c.GUID));
image.Save(imagesdir + c.GUID + "sm.jpg");

Check whether your IIS worker process has write permissions to the local file system, and in particular the folder whether you are saving the image.
Also, use Server.MapPath() to get the physical location.
See this blog for a solution.

Related

How to manage my directory's in asp.net using fileupload

Please take some of your time and read my problem
I need by using fileupload to upload just images to my web application
But when I select the image and click on upload button if the program see it a jpg file it will create a directory with the name jpg an save it there
That goes to gif and png
Please any one if you could give the code I will be very very thankful to you
Or at least link could help me
string folder = System.IO.Path.GetFileNameWithoutExtension(FileUpload1.FileName);
string path = Request.PhysicalApplicationPath + "/" + folder;
if (!System.IO.Directory.Exists(path))
System.IO.Directory.CreateDirectory(path);
string server_path = Request.PhysicalApplicationPath + "/myfiles/";
FileUpload1.SaveAs(server_path + FileUpload1.FileName);
You can use System.IO.Path.GetExtension(FileUpload1.FileName); to get the extension of the file.
Physical path for the virtual directory using:
System.Web.Hosting.HostingEnvironment.MapPath(virtualDirectoryPath)

C# Download an image from URL

I use a website to get stats on wifi usage. The website creates an image of a graph representation of the data. The way it does this is by the user, setting a date. So for example, lets say it was last months statistics. The website generates a URL which is then sent to the server and the server returns an image. an examples of the link is like this:
https://www.example.com/graph/daily_usage?time_from=2015-06-01+00%3A00%3A00+%2B0100&time_to=2015-06-30+23%3A59%3A59+%2B0100&tsp=1436519988
The problem is, I am making a third party program that will download this image to be used in my program. However, I cannot use the file. It is as if the file is corrupt or something. I have tried a few methods but maybe someone can suggest a different approach. Basically, how do I download an image that is generated by a server from a URL link?
P.S.
Just noticed that if I download the file by right clicking through a browser and save, the image downloads with a size of 17.something kilobytes. But if I use the WebClient method to download the image, it only downloads 1.5kb. Why would that be? Seems like the WebClient method does not download completely.
Currently my code
if (hrefAtt == "Usage Graph")
{
string url = element.getAttribute("src");
WebClient client = new WebClient();
client.DownloadFile(url, tempFolderPath + "\\" + currentAcc + "_UsageSummary.png");
wd.AddImagesToDoc(tempFolderPath + "\\" + currentAcc + "_UsageSummary.png");
wd.SaveDocument();
}
TempFolderPath is my desktop\TempFolder\
UPDATE
Out of random, I decided to see the raw data of the file with notepad and interestingly, the image data was actually a copy of the websites homepage html code, not the raw data of the image :S how does that make sense?
This will download the Google logo:
var img = Bitmap.FromStream(new MemoryStream(new WebClient().DownloadData("https://www.google.co.uk/images/srpr/logo11w.png")));
First of all, you have to understand link texture. If all links are same or close to each other, you have to use substring/remove/datetime etc. methods to make your new request link. For example;
string today = DateTime.Now.ToShortDateString();
string generatedLink = #"http://www.yoururl.com/image + " + today + ".jpg";
string generatedFileName = #"C:\Usage\usage + " + today + ".jpg";
WebClient wClient = new WebClient();
wClient.DownloadFile(generatedLink, generatedFileName);

Distributing and there is some problems

I will be distributing my program. It will take pictures and save them to a folder. The problem is: C:/Users/G73/Desktop/
Everyone has there own file path... In the code it is
bitmap.Save("C:/Users/G73/Desktop/My OVMK Photos//OpenVMK" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg", ImageFormat.Jpeg);
It has my File path and the name of my computer... How would I make it to change to the users path?
Try this code:
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
bitmap.Save(Path.Combine(path, "My OVMK Photos//OpenVMK", DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg"), ImageFormat.Jpeg);
It gets Desktop path for current user. You can get more special folders using Enviroment.SpecialFolder
To get the user's desktop -
Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
Which you would use with a Path.Combine - e.g.:
bitmap.Save (Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "My OVMK Photos//OpenVMK...
Though for images you'd likely be better off using the My Pictures directory -
Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
Try this
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));

Store uploaded images on Network using Ip address in c#

I am working on uploading a file from MVC and I want to store the uploaded images on the network or any other system,I have IP address of that system,
Can anyone help me to find out where I am making mistake or is there some other way to store image on certain IP address.
I am using this code to save image on another machine with IP address
public ActionResult Test(string ENROLLIMAGE)
{
if (!string.IsNullOrEmpty(ENROLLIMAGE))
{
HttpPostedFileBase file = Request.Files["ENROLLIMAGE"];
Guid ImageId = Guid.NewGuid();
var filename = ImageId.ToString() + Path.GetExtension(file.FileName);
file.SaveAs(Server.MapPath("\\\\192.168.11.113\\D:\\UploadedFiles" + filename));
Uri addy = new Uri("\\\\192.168.11.113\\D:\\UploadedFiles" + filename);
}
return View();
}
Kindly help !!!!
It is giving me error while I am trying to upload that "The given path's format is not supported."
Thanks
You can't use a drive specification like D: in an UNC path. What you should do is make a network share of the UploadedFiles folder on the remote machine, and use file.SaveAs("\\\\192.168.11.113\\UploadedFiles\\" + filename);
D: is not allowed in an UNC path, and will give you the error.
So, either use file.SaveAs("\\\\192.168.11.113\\<ShareNameHere>\\" + filename), or use file.SaveAs("\\\\192.168.11.113\\D$\\UploadedFiles\\" + filename) if you have administrative privileges on the target machine.

File.Exists using the wrong root path?

In my c# class I wrote I have a photo property that returns the photo source if the image exists (nothing or default image otherwise). In my code I use:
public string Photo
{
get
{
string source = "~/images/recipes/" + id + ".jpg";
if (File.Exists(source))
return "~/images/recipes/" + id + ".jpg";
else
return "";
}
}
If I get the FileInfo() information for this image I see that I tries to find this image in the following directory: C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\~\images\recipes
Of course the image is not located in that directory and File.Exists is returning me the wrong value. But how can I fix this?
Try this:
if(File.Exists(System.Web.HttpContext.Current.Server.MapPath(source)))
You need to map the relative path back to a physical path:
string source = HttpContext.Current.Server.MapPath("~/images/recipes/" + id + ".jpg");
You'll have to use:
Server.MapPath(source)
As you can not be 100% sure where the code will be running from, ie. it will be different in development and on a production server. Also are you sure ~/ works in windows? Wont that just be interpreted as a directory named ~? Unless thats what you want.

Categories