Set Dedault Picture For PictureBox And Save it C# - c#

I want to know how i can get the picture from the project folder and if User not choice any picture i can save it as Default persoanl picture
i use this code for create name , path and save ( if i have any pic eveything as fine, if user not choice any picture my code broken
//Set Image Name
string imageName = txtNationalCode.Text.ToString() + Path.GetExtension(imgPersonalPhoto.ImageLocation);
//Set Image Path
string ImageLocation = imgPersonalPhoto.ImageLocation;
//Save Image
string path = Application.StartupPath + "/Images/";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
try
{
if (imgPersonalPhoto.Image != null)
{
imgPersonalPhoto.Image.Save(path + imageName);
}
else
{
//i dont know how set some picture for default and save it ! ( i have 1 picture for background picturebox
imgPersonalPhoto.Image.Save(path + imageName);
}
}
catch
{
RtlMessageBox.Show("Add Picture for this Personal please ");
}
i will try add some picture in my app Folder
but if i send my app to other this folder not exist !

Set the image in Form_Load using Image.FromFile. This way it will always set the default image when the form is loaded.
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(#"C:\...");
}

Related

Find image path and view using PictureBox

I have a form application in which I can input a name of an image and view the image in a PictureBox after clicking a button.
private void button1_Click(object sender, EventArgs e)
{
string image = textBox1.Text + ".jpg";
PictureBox pictureBox1 = new PictureBox();
pictureBox1.ImageLocation = image;
}
This is my code but it doesn't do anything, the image I tried to search didn't appear in the PictureBox. What could possibly go wrong? Answers will be appreciated.
Because you've created a new instance of PictureBox but you didn't add it to your form. You should add it to your form's controls like this:
string image = textBox1.Text + ".jpg";
PictureBox pictureBox1 = new PictureBox();
//Set pictureBox1's location on the form
pictureBox1.Location = new Point(10 , 10);
//Add pictureBox1 to your form
Controls.Add(pictureBox1);
Now if your image variable contains a valid image path your PictureBox should show it.
Edit: To write a valid image path in your TextBox try write the full picture's path in your TextBox like below:
D:\Pics\yourPic
Or if you've added it to your project it should be like this:
D:\New folder (1)\WindowsFormsApplication1\WindowsFormsApplication1\yourPic
Just don't forget if you have already placed a PictureBox in your form you just need to call it in the code. You don't need to create a new one. And you should remove this line PictureBox pictureBox1 = new PictureBox();.

Image Control in Wpf showing previous image which was deleted

I have an image control which is showing the preview image. If the user delete the image (which resides in folder) it should show the newly taken image.
but the image control shows the deleted image instead of showing new image.
// clear the image source before deleting the image.
// save image in the directory
public string globalFilePath;
int imageCount = Directory.GetFiles(imgDir, "*", SearchOption.TopDirectoryOnly).Length;
string filePath = Path.Combine(imgDir, "IMAGE_" + ++imageCount + ".jpeg");
globalFilePath = filePath;
// setting image control source
var strUri = new Uri(WebCamControl.globalFilePath, UriKind.Relative);
previewImage.Source = BitmapFromUri(strUri);
//Method
public static ImageSource BitmapFromUri(Uri source)
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = source;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
return bitmap;
}
// delete image
previewImage.Source = null;
if (System.IO.File.Exists(WebCamControl.globalFilePath))
{
System.IO.File.Delete(WebCamControl.globalFilePath);
}
else
{
MessageBox.Show("File Not Exists");
}
After deleting the image in the directory file the image image control should show the new image, but my Image control shows the deleted image. please give me the solution.
In WPF, we generally don't need to use actual BitMapImage objects to display an Image. It's far easier to let the .NET Framework convert our string file paths to the images to the actual Image elements.
Also, it is far better to data bind the Image.Source to a property that implements the INotifyPropertyChanged interface (or a DepenedencyProperty):
<Image Source="{Binding YourImageFilePath}" ... />
Then, whenever a file path of a new image is set to the YourImageFilePath property, your displayed image will update immediately.
YourImageFilePath = filePath;
Try This one:
string path = ((BitmapImage)img.Source).UriSource.LocalPath;
img.SetValue(System.Windows.Controls.Image.SourceProperty, null);
File.Delete(path);
OR
string path = ((BitmapImage)img.Source).UriSource.LocalPath;
img.Source = null;
File.Delete(path)

Populating picturebox from datagridview row

private void accView_CellClick(object sender, DataGridViewCellEventArgs e)I have an application with a database. It is an inventory application. When the user goes to add an entry to a table they have the option to upload a photo. It is not required. Once they add the new item without a photo successfully then should be able to go to the main form and select the row on the datagridview and it will populate some labels with data based on what is selected.
The problem starts when they select a row without a photo uploaded to the database. It throws an ArgumentException stating that The path is not of a legal form. Below is my code. I am having a hard time getting around this. If a row is selected that does not have a photo, the picture box should just show the default photo...
private void accView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1)
return;
if (!accView.Rows[e.RowIndex].IsNewRow)
{
//Visual Studio shows the error on the line of code below.
accPictureBox.Image = Image.FromFile(accView.Rows[e.RowIndex].Cells[10].Value.ToString(), true);
}
}
Update:
private void accView_CellClick(object sender, DataGridViewCellEventArgs e)
{
string defImage = (new System.Uri(System.Reflection.Assembly.GetEntryAssembly().CodeBase)).AbsolutePath;
string imageDir = Path.GetDirectoryName(defImage);
string fullPath = Path.Combine(imageDir, #"C:\Users\Brandon\Documents\Visual Studio 2013\Projects\Firearm Asset Management\Firearm Asset Management\bin\Beta\Images\DefaultImage4.jpg");
var path = accView.Rows[e.RowIndex].Cells[10].Value;
//Syntax Error on line below for invalid arguments.
if (string.IsNullOrEmpty(path))
{
//set the default path
path = fullPath;
}
//Syntax Error on line below for invalid arguments.
accPictureBox.Image = Image.FromFile(path, true)
}
Where there's no image uploaded, then presumably
accView.Rows[e.RowIndex].Cells[10].Value
will be null or an empty string. Therefore, check
string defImage = (new System.Uri(System.Reflection.Assembly.GetEntryAssembly().CodeBase)).AbsolutePath;
string imageDir = Path.GetDirectoryName(defImage);
string fullPath = Path.Combine(imageDir, #"./Images/DefaultImage4.jpg");
string path = string.Empty;
if (accView.Rows[e.RowIndex].Cells[10].Value != null)
path = accView.Rows[e.RowIndex].Cells[10].Value.ToString();
//Syntax Error on line below for invalid arguments.
if (string.IsNullOrEmpty(path))
{
//set the default path
path = fullPath;
}
//Syntax Error on line below for invalid arguments.
accPictureBox.Image = Image.FromFile(path, true)
I imagine that cell[10] is where the image path should be. However, if the user has not uploaded an image you will not have a path.
I would first check the value of cell[10] and if the value is a properly formed path then I would assign it to accPictureBox.Image

Detecting which image is used in an imagebox

I'm trying too make a memory game.
In this game when a button is klicked the button and an picturebox will be send into a List.
I want too use the images inside the pictureboxes as a way too use this code. But even when the two images are the same the code wont work. Is there a way too check the image used like Name.jpg.
if(buttonCount == 2)
{
if(pictureList[0].Image == pictureList[1].Image)
{
buttonCount = 0;
buttonList.RemoveAt(0)
buttonList.RemoveAt(0);
pictureList.RemoveAt(0);
pictureList.RemoveAt(0);
}
}
You could save an Id of the image (or e.g. the filename like you suggested) in the Tag.
So when loading the image into the picture box:
string path = "PATH";
pictureBox.Image = Image.FromFile(path);
pictureBox.Tag = path;
Then you could compare the Tag.
BUT I think (show us how you load the images please) this is not working as it is, because you load the image twice from the disk like:
pictureBox1.Image = Image.FromFile(path);
pictureBox2.Image = Image.FromFile(path);
Because then you have differen instances and so the equals returns false.
If you do it like the following it should also work:
var image = Image.FromFile(path);
pictureBox1.Image = image;
pictureBox2.Image = image;
In your current application, you do not have enough information associated with the image object to identify it. As such, you need to possibly extend the Image class to include this information or store it in some other way for comparison.
Extending Image class
public class GameImage : Image
{
public static GameImage GetImage(string filename)
{
GameImage img = (GameImage)Image.FromFile(filename);
img.FileName = filename;
return img;
}
public string FileName { get; private set; }
}
Then the comparison becomes
if(buttonCount == 2)
{
if(((GameImage)pictureList[0].Image).FileName == ((GameImage)pictureList[1].Image).FileName)
{
buttonCount = 0;
buttonList.RemoveAt(0)
buttonList.RemoveAt(0);
pictureList.RemoveAt(0);
pictureList.RemoveAt(0);
}
}
Note: Note tested!

Displaying multiple images with image control in asp.net 3.5

I want to display all the images from given directory. For that i have given image controls to display images, but i want to display images control with that url automatically as per images in that directory. Suppose in my directory 5 images are present then 5 image controls should be display in my button click event.
I have written code in button_click event that displays only two images from the given directory is as follows :
protected void btncompare_Click(object sender, EventArgs e)
{
Bitmap searchImage;
searchImage = new Bitmap(#"D:\kc\ImageCompare\Images\img579.jpg");
string dir = "D:\\kc\\ImageCompare\\Images";
DirectoryInfo dir1 = new DirectoryInfo(dir);
FileInfo[] files = null;
files = dir1.GetFiles("*.jpg");
double sim;
foreach (FileInfo f in files)
{
sim = Math.Round(GetDifferentPercentageSneller(searchImage, new Bitmap(f.FullName)), 3);
if (sim >= 0.95)
{
string imgPath = "Images/" + files[0];
string imgPath1 = "Images/" + files[1];
Image1.ImageUrl = "~/" + imgPath;
Image2.ImageUrl = "~/" + imgPath1;
Response.Write("Perfect match with Percentage" + " " + sim + " " + f);
Response.Write("</br>");
}
else
{
Response.Write("Not matched" + sim);
Response.Write("</br>");
}
}
}
Seems like you're trying to fit a square peg into a round hole here. The Image control is designed to show a single image.
If you want to display multiple images, use multiple image controls. I suspect that the reason you're not doing this is because you don't know how many images that you're going to need to display.
If I had the same problem, I would turn the contents of the image directory into something that could be bound onto a Repeater. Each ItemTemplate would contain its own Image control, allowing you to display as many images as you need without resorting to hackery.

Categories

Resources