Where do my images go when i use FileUpload1.SaveAs(Server.MapPath())? - c#

Originally I started having the images put into a database and using generic handler to view the images, it was ok for one part of my app but then I realized that storing images in the database wasn't going to work out to well for a part of my application because I will be dynamically creating an html table with hyperlinks and using images for hyperlinks.
So using generic handlers would see to be a huge mess when creating the html and navigation hyperlinks,
So what I have opted to do is now is put these images to a folder on the server, but right now I am using my laptop before I even get to the point of publishing the app on line.
So this is the code I am using...
string iconName = fpIcon.FileName;
string iconExtension = Path.GetExtension(iconName);
string iconMimeType = fpIcon.PostedFile.ContentType;
string[] matchExtension = { ".jpg", ".png", ".gif" };
string[] matchMimeType = { "image/jpeg", "image/png", "image/gif" };
if (fpIcon.HasFile)
{
if (matchExtension.Contains(iconExtension) && matchMimeType.Contains(iconMimeType))
{
fpIcon.SaveAs(Server.MapPath(#"~/Images/" + iconName));
Image1.ImageUrl = #"~/Images/" + iconName;
}
}
So my question is, and don't laugh to hard but, where are these images being stored? I can't find them anywhere on my laptop using the windows search. With all the images that I have in this ~/Images directory, I can change the image to any image I wanted by supplying the name, but I have no idea where the images are being held, and when I do deploy to a site then where are they going to be then?
Thanks

Server.MapPath gives your current website directory(IIS hosted or running from VS). So go to your website directory, find Images directory there and you'll see your images.

Related

c# embedded images or is there another way

I'm trying to find a good way to load say 100+ small images (98px png's each) into my .net form and additionally have them all interactive in a way (click, and a name of the image/file), but without loading the images from a local predefined folder. What do I mean by that? Let me explain and demonstrate an example. Please bear with me, I'm kinda confused on the topic of building the solution and compiling the executeable's folder structure.
Example:
Imagine when creating a profile on say, here for example. Usually, you set a name and add a profile picture. Sometimes the profile picture has to be uploaded, but in my case I want to have a preset of 100 available profile pictures that the user can choose from. It may sound like a lot, but considering the purpose of the application it really is nesseary.
The problem/question:
The easy and probably not so good way is simply adding pictureboxes and assigning a click to them onto a flowlayoutpanel. This is my current temporary solution, but the big flaw is that it uses a locally stored path. This app is supposed to installed on several machines and I need this to work on whichever computer it's installed on, no matter what drive letter that is used ect.
string[] files = Directory.GetFiles("C:\\path\\folder_a", "*", SearchOption.TopDirectoryOnly);
foreach (var filename in files)
{
Bitmap bmp = null;
try
{
bmp = new Bitmap(filename);
}catch{
}
var card = new PictureBox();
card.BackgroundImage = bmp;
card.Tag = (filename.Split('\\').Last());
card.Padding = new Padding(0);
card.BackgroundImageLayout = ImageLayout.Stretch;
card.Size = normal;
card.Click += delegate
{
//something
};
flowProfileIcons.Controls.Add(card);
}
These are the options to my knownledge, but I don't really know:
Embed (in 'build action') all the images directly from a folder within solution explorer and use AssemblyDirectory to find and use the images in a similar way as displayed above. However, will this allow the users to actually access and potentionally modify/delete the files because they are exposed in the installation folder? And is this even a good way to do this?
Put all images in resources.resx and somehow use the images from there, but as far as I've seen that isn't very easy to do. I will definitely look into it though if this is the prefered way. I did try some things out and this is my progress so far, though just testing - not working.
var images = Properties.resProfileIconsDark.ResourceManager
.GetResourceSet(CultureInfo.CurrentCulture, true, true)
.Cast<DictionaryEntry>()
.Where(x => x.Value.GetType() == typeof(Bitmap))
.Select(x => new { Name = x.Key.ToString(), Image = x.Value })
.ToList();
This is where I'm at. Hopefully someone can push me in the right direction.

Saving images from IP Camera to folder in application

I have a camera connected to a network that provides images which can be accessed by IP. For example something like this.
http://170.1.2.3/image?camera=2
I get returned a jpg file, which I can easily display back on a webpage using normal html.
<img src="http://170.1.2.3/image?camera=2" />
But instead of simply showing the camera image as seen from the time of the page load, I also want to save this image locally to the server using an MVC4 application. In addition it would be nice to include a timestamp on the filename. I'm not having any luck finding an example that looks right to me.
Let's say I want to start with the most basic example:
string imgUrl = "http://170.1.2.3/image?camera=2";
savelocally(imgurl, "newfilename-"+DateTime.Now.ToString());
.. and savelocally() should place the file in "~/Uploads". How do I do this? All the examples I've come across have been for uploading a file from desktop. I don't know how to translate that into grabbing a remote image.
Update:
This is an incomplete answer, as pointed out by comments.
string url = "http://170.1.2.3/image?camera=2";
string localFilename = #"C:\Projects\MvcApplication1\MvcApplication1\Uploads\tofile.jpg";
using (WebClient client = new WebClient())
{
client.DownloadFile(url, localFilename);
}
It works, however I don't like the fact that the file path is an absolute one. I tried replacing localFilename with ~/Uploads/tofile.jpg but it results in an error on execution. If possible I want to avoid having a fixed location for the upload folder.

Converting aspx page to image file

I have c# dynamic aspx page after new property add I create for record brochure
http://veneristurkey.com/admin/Brochure.aspx?Admin=PropertiesBrochureA4&id=36
but I want to this convert image file I am searching on internet but all with webbrowser and windows forms. I need on page load show not css type also image file. jpg, png or tiff how i can do this. i need to see sample code..
saving aspx page into an image 2
As I mentioned in my comment, your best bet is to opt for attempting to render HTML to an image.
Here is the link for a library that will allow your to render html to an image:
http://htmlrenderer.codeplex.com/
Here is code that does exactly what you're asking:
http://amoghnatu.wordpress.com/2013/05/13/converting-html-text-to-image-using-c/
Now all you have left is to get the html, since I'm assuming you don't want this to render to the browser prior to generating this image - you should look into grabbing the rendered html from the aspx page on the server prior to returning it, and then just return the image. To render a page:
https://stackoverflow.com/a/647866/1017882
Sorted.
If you do not mind using a commandline tool you can have a look at wkhtmltopdf. The package include a wkhtmltoimage component that can be used to convert HTML to image, using
wkhtmltoimage [URL] [Image Path]
Codaxy also wrote a wkhtmltopdf c# wrapper available through the NuGet package manager. I'm not sure if the wkhtmltoimage component was included, but it should be easy enough to figure out how they wrap the wkhtml components.
i fixed my problem with screenshot machine API they are my code..
public void resimyap()
{
var procad = WS.Satiliklars.Where(v => v.ForSaleID == int.Parse(Request.QueryString["id"])).FirstOrDefault();
var imageBytes = GetBytesFromUrl("http://api.screenshotmachine.com/?key=xxxxxx&size=F&url=http://xxxxxxx.com/a4.aspx?id=" + procad.ForSaleID);
string root = Server.MapPath("~/");
// clean up the path
if (!root.EndsWith(#"\"))
root += #"\";
// make a folder to store the images in
string fileDirectory = root + #"\images\a4en\";
// create the folder if it does not exist
if (!System.IO.Directory.Exists(fileDirectory))
System.IO.Directory.CreateDirectory(fileDirectory);
WriteBytesToFile( fileDirectory + + procad.ForSaleID + ".png", imageBytes);
Yes i also try wkhtmltopdf c# wrapper but in pdf or image converting time my computer fan goin crayz. also i must upload server exe file and my hosting firm didnt support them

Saving images relative to content directory where my site is running in IIS

I want to save images uploaded from a asp mvc page to the content/img/ folder which is relative to where my site is running in IIS. But I don't want to hard code the location on my file system to a Absolute path as this could change easily. This is the code I'm using to save them.
public static string SaveCompanyLogoImage(HttpPostedFileBase file)
{
var newFileName = GetNewCompanyLogoFileName(file.FileName);
file.FileName = newFileName;
file.SaveAs(//relative-location-here);
return Path.GetFileNameWithoutExtension(last);
}
Hope this explains my issue!
Thanks for any assistance
Use Server.MapPath("~/MyImagesFolder")
http://msdn.microsoft.com/es-es/library/system.web.httpserverutility.mappath.aspx

Access folders in root directory

Hi I am developing an asp.net web application. I have to access one of the image in images folder in root directory. I am using following code in my code behind file.
string imageDirectory = HttpContext.Current.Server.MapPath("~/images/");
string imageUrl = imageDirectory + "/img1.bmp";
This works fine in my local machine. My question is does this code work when I move my application to production ?
It should as long as you have an application root/virtual directory for your site.
Also, you can combine these two lines into:
string imageUrl = HttpContext.Current.Server.MapPath("~/images/img1.bmp");
If you're thinking of putting imageUrl into an <img> tag, then no, it won't work. Server.MapPath will return your file or directory as a local Windows file/directory name, so something like "C:\WebRoot\MyWebApplication". If you send this to the browser, obviously, the browser won't pick up the image.
What you can do is something like:
string imageUrl = ResolveClientUrl("~/images/myImage.gif");

Categories