Retrive image file from folder and display into Image Control - c#

I have save my Image using Below code In a Folder in my source file..
now I have to retrvive the same saved Image on my Form
string filename = Path.GetFileName(fileupload1.PostedFile.FileName);
string strtemp = System.DateTime.Now.ToString("ddMMyyhhmmss_") + filename;
fileupload1.SaveAs(Server.MapPath("Image/" + strtemp));
So how should i give a Path to my Image Control
I have tried something like this to get a path of file and folder but simply i cant fetch the image from it into my image folder
Image2.ImageUrl = (Server.MapPath("Image/" + strtemp));

Use it like this:
Image2.ImageUrl = "~/Image/" + strtemp;
Server.MapPath is used to get the physical path of a resource of your server. You need it for operations like saving files. However your physical path is not valid on the web. You should use the virtual path to specify the url.

You can fetch Image like this.:-
Image2.ImageURL = "~/Image/"+strtemp;
Hope this helps you.

Image2.ImageUrl = "~\\Image\\" + strtemp;
Use the above code.

if(!(DropDownList1.SelectedItem.Text==""))
{
string a = DropDownList1.SelectedItem.Text;
string [] q = a.Split('/');
string qq = q[1];
Image1.Visible = true;
Image1.ImageUrl = "~/Images1/" + qq;
}

Related

Uploaded Image's Path is Broken in ASP.NET - unable to show Image

Previously I successfully showed Image like this:
<img class="centered" src="~/Content/images/gif.gif" style="max-height:12vw;" />
with no problem, but this image was added to the Solution's images folder.
However, when I upload image to API like and store it like this:
string FileName = valueobject.PictureName;
string path = HttpContext.Current.Server.MapPath("~/UserFiles/" + valueobject.PictureOwner + "/");
string imgPath = Path.Combine(path, FileName);
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
File.WriteAllBytes(imgPath, valueobject.PictureImage);
//store UserFile with path to the image
UserFile _userFile = new UserFile();
_userFile.FileOwner = valueobject.PictureOwner;
string relativePath = "~/UserFiles/" + valueobject.PictureOwner + "/";
string imgRelativePath = Path.Combine(relativePath, FileName);
_userFile.FilePath = imgRelativePath;
The image is saved - the folder is created and image is saved correctly in localhost.
However I am unable to resolve the relative path in MVC when I try:
<img height="400" style="width:auto" src="#Model.HomeGalleries[g].GalleryImageStrings[i]" runat="server" />
I get "broken" image icon even that
#Model.HomeGalleries[g].GalleryImageStrings[i]
is equal to
imgRelativePath
I am not sure why this does not work.
you must include Server.MapPath for this actual image path. folloing below this line ,like as.
string relativePath = Server.MapPath("~/UserFiles/" + valueobject.PictureOwner + "/");
please try this.hopefully this you help.

Moving and renaming image from one folder to another?

I have two folders as part of my project, in the folder "Images" is a file called "FingerprintScan.jpg". What I am trying to do is fetch this file and then save it in my other folder called "FingerPrints".
The code I am using does not throw any errors and as far as I can tell should logically work however nothing happens.
string fileName = "FingerprintScan.JPG";
string newfilename = TextBoxUsername.Text + LabelStudentID.Text + ".JPG";
string appPath1 = AppDomain.CurrentDomain.BaseDirectory + #"Images\";
string appPath2 = AppDomain.CurrentDomain.BaseDirectory + #"FingerPrints\";
string sourceFile = System.IO.Path.Combine(appPath1, fileName);
string destFile = System.IO.Path.Combine(appPath2, newfilename);
System.IO.File.Copy(sourceFile, destFile, true);
I have tried playing around and using #"~\Images\ and #"Images but had no luck.
Try this combination and make sure your directory name and source file name match with the physical directory and file
string appPath1 = AppDomain.CurrentDomain.BaseDirectory + "Images";
string appPath2 = AppDomain.CurrentDomain.BaseDirectory + "FingerPrints";

Create a copy of a file in same location as original

The end-user supplies a path, indicating where the original document is.
string DocxFileName = "C:\\WorksArshad\\3.docx";
I'd like to create a copy of the document name 3.docx as 3Version1.docx and store the copy in the same directory as the original.
How do I get the whole path without the file name and extension?
(i.e.) I need to get the "C:\\WorksArshad\\" path alone.
FileInfo file = new FileInfo(Session.FileName);
string path = file.Directory.tostring();
and then using
string fileName = Path.GetFileNameWithoutExtension(Session.FileName);
string DocxFileNamee = path + "\\" + fileName + "V1.docx";
File.Copy(Session.FileName, DocxFileNamee, true);
where in Session.FileName = "C:\WorksArshad\3.docx" and in path I'd get "C:\WorksArshad"
requirement solved .
Or
File.Copy(Session.FileName, Path.Combine(Path.GetDirectoryName(Session.FileName)
, Path.GetFileNameWithoutExtension(Session.FileName)+"V1.docx"),true);
both the above gives the solution

changing name of uploaded images

What I am trying to achieve is changing the name of uploaded images to a somewhat unique string.
I have created an image gallery which is populated dynamically by database. The gallery is working fine except, that files stored in the database will be problematic if an image with the same name is uploaded!
Code in the controller for file upload:
string AdvertImage = picture1.FileName;
advert.AdvertImage = AdvertImage;
var image1Path = Path.Combine(Server.MapPath("~/Content/Images"), AdvertImage);
picture1.SaveAs(image1Path);
The code below is what I am working on
string BackImage = DateTime.Now.ToString("yyyyMMddhhmmssfffffff");
caradvert.BackImage = BackImage;
var image3Path = Path.Combine(Server.MapPath("~/Content/CarImages"), BackImage);
picture3.SaveAs(image3Path);
I have managed to create a unique file name thou it is not appending .jpg to the end of the image.
Any help or advice on the subject welcome
Try this:
//save file in folder
if (FileUpload1.PostedFile.ContentType.ToLower().StartsWith
("image") && FileUpload1.HasFile)
{
string savelocation = Server.MapPath("savedImages/");
string fileExtention = System.IO.Path.GetExtension(FileUpload1.FileName);
//creating filename to avoid file name conflicts.
string fileName = Guid.NewGuid().ToString();
//saving file in savedImage folder.
string savePath = savelocation + fileName + fileExtention;
FileUpload1.SaveAs(savePath);
}

How to dynamically change backgroundimage in C#.Net?

Now I'm trying to change dynamically MainForm's backgroundimage.
I wrote that following code segment...
this.BackgroundImage = Image.FromFile("Bar1.png");
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
Image that I want to change is located in my current project.
But I don't know how to use FromFile Method?
Try something like this:
string path = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
string filename="yourfilename";
this.BackgroundImage = Image.FromFile(Path.Combine(path ,filename));
or:
string customPath = "d:\testpath";
string filename="yourfilename";
this.BackgroundImage = Image.FromFile(Path.Combine(customPath ,filename));
You can get application startup path with this code:
Application.StartupPath + "\yourimage"
or you can use
System.Reflection.Assembly.GetExecutingAssembly().Location + "\yourimage";
Please read documentation about FromFile method here.
And if you have image in your resource file, you can access it like this:
this.BackgroundImage = Properties.Resources.yourImageName;
OpenFileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
this.BackgroundImage = Image.FromFile(dialog.FileName);
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
}
make a directory named background where your exe located.
copy background jpg file in that directory
add following in form load event
string path = System.IO.Directory.GetCurrentDirectory() + "\background\";
string filename="back.jpg";
this.BackgroundImage = Image.FromFile(Path.Combine(path, filename));
if you changed background jpg file keeping same file name, the background will be changed.

Categories