This is my image uploader that uploads straight to the folder i want including thumbnails.
So what i want to do is to write for each time i upload a picture it always write it's path in my database table for example: here is the picture how i want it to look like:
http://i.stack.imgur.com/wbHoO.jpg
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileName = FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(Server.MapPath("././Data/Normal/") + fileName);
System.Drawing.Image img1 = System.Drawing.Image.FromFile(MapPath("././Data/Normal/") + FileUpload1.FileName);
System.Drawing.Image bmp1 = img1.GetThumbnailImage(680, 450, null, IntPtr.Zero);
bmp1.Save(MapPath("././Data/Thumbnails/") + FileUpload1.FileName);
}
PhotoTableAdapter PhotoInfo = new PhotoTableAdapter();
PhotoInfo.InsertPhoto(TextBox1.Text, TextBox2.Text, TextBox3.Text,TextBox4.Text);
}
After the TextBox4.Text, i should add something to write the path for this table:
http://i.stack.imgur.com/v3Vwh.jpg
It would be great if i can get some help thank you
i'm assuming that you have updated your DataModel to get the newly added columns in Database they are Path_Thumbnail,Path_L
if you have updated your DataModel you will get following uodated Query in your Model :
INSERT INTO Photo (Name, Description, Caption, TagID, Path_Thumbnail, Path_L) VALUES (#Name,#Description,#Caption,#TagID,#Path_Thumbnail,#Path_L);
Now while sending the Data to Insert Command add the both ThumbnailPath and ImagePath sothat along with normal feilds both images paths will be saved into Database.
Replace this:
PhotoInfo.InsertPhoto(TextBox1.Text, TextBox2.Text, TextBox3.Text,TextBox4.Text);
With following:
PhotoInfo.InsertPhoto(TextBox1.Text, TextBox2.Text, TextBox3.Text,TextBox4.Text,Server.MapPath("././Data/Thumbnails/") + FileUpload1.FileName,Server.MapPath("././Data/Normal/") + FileUpload1.FileName);
if you want to save only Relative Path of the images(only path from project folders) use following code:
PhotoInfo.InsertPhoto(TextBox1.Text, TextBox2.Text, TextBox3.Text,TextBox4.Text,"././Data/Thumbnails/" + FileUpload1.FileName,"././Data/Normal/" + FileUpload1.FileName);
Related
I have an image file named image1.jpg in picture folder. This image file is protected by zip and the password is 1234. I need to load this image to my C# winform application; So I have to unzip this image, load it and then delete the image1.jpg again. My problem is that my C# application doesn't show the image when I delete it even after loading the image. If I remove "File.Delete(...." line as you see, It shows the image and there is no problem.
//Unzip a zip file protected and overwrite if needed
using (ZipFile zip = ZipFile.Read(Directory.GetCurrentDirectory().ToString() + "\\Pictures" + "\\image1.zip"))
{
zip.Password = "1234";
zip.ExtractAll(Directory.GetCurrentDirectory().ToString() + "\\Pictures\\", Ionic.Zip.ExtractExistingFileAction.DoNotOverwrite);
}
pictureBox1.ImageLocation = Directory.GetCurrentDirectory().ToString() + "\\Pictures" + "\\image1.jpg";
File.Delete(Directory.GetCurrentDirectory().ToString() + "\\Pictures" + "\\image1.jpg");
var imagePath = Directory.GetCurrentDirectory().ToString() + "\\Pictures" + "\\image1.jpg;
pictureBox1.LoadCompleted += (s, e) => File.Delete(imagePath);
pictureBox1.ImageLocation = imagePath;
i wanna create backup data in my application I used saveFileDialog, so i can place backup file anywhere i want (Dekstop, drive D, etc)
my backup file will be db, image, video so i guess it's will be easier to place that in one folder let say it's "myBackup" folder (generate automatically with C#)
so if user wanna save in Dekstop all of backup data will be in ~C:\Users\Maju\Desktop\myBackup~
i already successfully generate folder but my file won't save inside that
mySaveFileDialog.FileName = "Backup Database " + dateTimeNow;
if (mySaveFileDialog.ShowDialog() == DialogResult.OK)
{
string fileAsal = System.IO.Path.Combine(Global.myDatabaseLocation, "data.mdb");
FileInfo fi = new FileInfo(mySaveFileDialog.FileName);
string nameFolder = "myBackup";
System.IO.Directory.CreateDirectory(#fi.DirectoryName + "\\" + nameFolder);
string path = System.IO.Path.Combine (fi.DirectoryName, "\\" + nameFolder);
string pathDestination = System.IO.Path.Combine(path, mySaveFileDialog.FileName);
System.IO.File.Copy(fileAsal, pathDestination, true);
}
Is not it easier to use FolderBrowserDialog?
mySaveFileDialog.FileName already includes the path to the file so you need to write
string pathDestination = System.IO.Path.Combine(path, System.IO.Path.GetFileName(mySaveFileDialog.FileName));
I am facing a very strange issue of renaming images (please see screenshot)
when i click up/down buttons i rename both images to swap them.
first two three times it works perfectly, but when i repeat this process it stops changing the images in the browser.
Further investigating i found that my code is working pefectly but the browse is caching the images that's why they dont change their position.
when i press cTRL + F5 i see changed images or if i move to any other page and come back to same page i again see right order of images, but on same page it does'nt happen :(
any one can put some light on it? how can i resolve this issue?
Code of swapping:
protected void rptImages_ItemCommand(object source, RepeaterCommandEventArgs e)
{
int id = Convert.ToInt32(Request.QueryString["Id"]);
String path = Server.MapPath(e.CommandArgument.ToString());
if (e.CommandName == "Up")
{
SwapImagesOnUpClick(id, path);
}
if (e.CommandName == "Down")
{
SwapImagesOnDownClick(id, path);
}
}
public void SwapImagesOnUpClick(int id, string path)
{
string oldFileName;
string newFileName;
string tempFileName;
string basicPath = path.Substring(0, path.LastIndexOf('\\'));
oldFileName = path.Substring(path.LastIndexOf('\\') + 1);
tempFileName = "temp.jpg";
//get new filename
string[] fileParts = oldFileName.Split('.');
newFileName = (Convert.ToInt32(fileParts[0]) - 1).ToString();
string newFilePath = basicPath + "\\" + newFileName + ".jpg";
string tempFilePath = basicPath + "\\" + tempFileName;
// set already existing newfilename to temp
File.Move(newFilePath, tempFilePath);
File.Move(path, newFilePath);
File.Move(tempFilePath, path);
}
I suggest that you to change the location of image1 to location image2 inside the generated page instead of renaming the files
if somebody have to face similar situation as mine just append: "?timestamp"
in image src in this way your image wont get cached and everything will work perfectly to achieve up and down image functionality using ajax.
I have a field in my app (c#) to save an image into the database. I written the following code to save the image into a folder and then save the path into the database. But the image is not getting saved into the folder.
string imgName = FileUpload1.FileName.ToString();
string imgPath = null;
if (imgName == "")
{
//int taxiid = Convert.ToInt32(HiddenField1.Value);
Taxi t = null;
t = Taxi.Owner_GetByID(tx.Taxi_Id, USM.OrgId);
imgPath = t.CarImage;
}
else
{
imgPath = "ImageStorage/" + imgName;
}
FileUpload1.SaveAs(Server.MapPath(imgPath));
tx.CarImage = imgPath;
I think your problem is that you add the name to the Path I try it and for me it works fine if I save it like this:
FileUpload1.SaveAs(Server.MapPath("ImageStorage") + imgName);
And as #Rahul mentioned add a try catch to prevent errors.
And you check
if (imgName == "")
According to my understanding it's not posible that imgName is "" but anyway you better add a check if the fileupload has a file.
if (FileUploadControl.HasFile)
filename = Path.GetFileName(FileUpload.FileName);
HttpPostedFile pf = FileUpload.PostedFile;
System.Drawing.Image img2 = System.Drawing.Image.FromStream(pf.InputStream);
System.Drawing.Image bmp2 = img2.GetThumbnailImage(200, 210, null, IntPtr.Zero);
Imagename = objUser.UserID + filename;
Imagepath = "D:\\Shopy_Web_21-6-12\\Shopy\\Images" + Imagename;
bmp2.Save(Path.Combine(#"D:\Shopy_Web_21-6-12\Shopy\Images", Imagename));
I've converted the file upload in to two thumbnails and saved them locally, but now I need retrieve the image to display it on the user's profile. How can I get the image to display from where I've stored it?
You can create a virtual path in your application say: ..Images/ and save your images in that folder.
and after that use the below one to fetch the image URL:
string str = Server.MapPath("Images/" + Filename);
Now you will get the url to your image that can be displayed directly.
Link your images folder (...\Shopy\Images) to a virtual folder under your app so then you can link to them within <img> elements.