Delete an image programmatically after loaded in pictureBox completely in C# - c#

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;

Related

picturebox.image saving overwrite in c#

i have a project with a picturebox that by clicking on a button , i save picturebox.image , in a folder with "scan" name .
and if i insert an image with repetitive name in picturebox , when i save image , program overwrite the image in the folder .
but i want when i insert a same name image to the folder "scan" , i use a messagebox with message : " name of image is repetetive . please change the name of image " . help me please ..
here is my saving code :
pictureBox1.Image.Save(Application.StartupPath + "\\scan\\ " + txtDesc11.Text + ".jpg");
You need to check if the file currently exists before you save. I encourage you, instead of just asking us to solve your problem you instead search "How do i check if a file exists?" and then try to solve the problem yourself.
string filePath = Application.StartupPath + "\\scan\\ " + txtDesc11.Text + ".jpg";
if (File.Exists(filepath))
{
// show a message to the user
}
else
{
pictureBox1.Image.Save(filePath);
}

C#: FileNotFoundException when loading images from resource

I am trying to load in a bunch of images from my resource file but I am getting the FileNotFoundException for some reason. the Image names are like so:
"image01.png", "image02.png", ... , "image10.png", image11.png"
In the end I want to be able to display all of the images on the screen.
Here is what I have:
String imgName;
int row = 0, col = 0;
for (int i = 1; i <= 15; i++)
{
//get the name of the current image
if (i < 10)
imgName = "image0" + i + ".png";
else
imgName = "image" + i + ".png";
Image img = null;
try {
img = Image.FromFile(imgName);//read the image from the resource file
}
catch (Exception e) { Console.WriteLine("ERROR!!!" + e); }
}
Here is a sample error output that I am getting:
ERROR!!!System.IO.FileNotFoundException: tile01.png
at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement)
at System.Drawing.Image.FromFile(String filename)
Screenshot:
I have also fixed a type on line 56 from: "PictureForm.PuzzleForm." to "PicturePuzzle." but still no luck.
You are not specifying a path to load the file from. They will be loaded from where the assembly is running.
Note that Image.FromFile does not load an embedded resource, but rather the .png from disk. I assume this is what you intend.
Check the properties for the images in Visual Studio and ensure that Copy to Output Directory is Copy if Newer or Copy Always. Here's a screenshot (in my case it's a cursor resource, but same idea for an image).
UPDATE
If you have embedded your images in your EXE or another file, you can use code similar to
System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file =
thisExe.GetManifestResourceStream("AssemblyName.ImageFile.jpg");
this.pictureBox1.Image = Image.FromStream(file);
(Source)
NOTE
You can either embed your images in a binary file (commonly your .exe) by setting the property Build Action to Embedded Resource, or leave them as separate files by setting Build Action to Content. If you leave as content, set Copy to Output Directory to True.
There's nothing in your code to say where the files are located so it's defaulting to somewhere the files aren't. If the files sit in the same location as your exe then try something like
imgNmae = "./image0" + i + ".png";
adjusting the relative path to account for where the files actually sit.

Writing the path of the uploaded pictures in database

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);

Caching issue in C# renaming the file

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.

Image saving into database from a folder and get back into the UI

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)

Categories

Resources