Add an image into the resources file - c#

I have a picture box where i display an image but i couldn't find the way to save it in the resources.
Here is my code so far:
private void imgContact_Click(object sender, EventArgs e)
{
// open file dialog
OpenFileDialog open = new OpenFileDialog();
// image filters
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
// display image in picture box
imgContact.Image = new Bitmap(open.FileName);
// image file path
string imagePath = open.FileName;
}
}

Related

Load images in Image Button

I am using below code to load image in my image button. But the Image is not loading in Button. However, I'm not receiving any kind of error.
XAML Code:
Image Name="imgPhoto" HorizontalAlignment="Left" Height="160" Margin="10,41,0,0" VerticalAlignment="Top" Width="164"/>
Button Content="Load" HorizontalAlignment="Left" Margin="191,67,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
Below is the code to load the Image in Image Button.
Button Click Event Code:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Image files (*.jpg)|*.jpg|All Files (*.*)|*.*";
if (dlg.ShowDialog() == true)
{
string selectedFileName = dlg.File.Name;
imgPhoto.Source = new BitmapImage(new Uri(selectedFileName, UriKind.Relative));
}
}
with OpenFIleDialog we don't have File.Name we have only FileName property which Gets or sets a string containing the file name selected in the file dialog box.
check the below code
OpenFileDialog objOpenFileDialog = new OpenFileDialog();
objOpenFileDialog.Filter = "Image Files(.jpg)|*.jpg;*.gif;*.png";
if (objOpenFileDialog.ShowDialog() == true)
{
imgPhoto.Source =new BitmapImage(new Uri(objOpenFileDialog.FileName));
}
The problem is here: UriKind.Relative.
If you're building URI from file name, picked up from system's file dialog, then it is absolute URI:
myImage.Source = new BitmapImage(new Uri(fileDialog.FileName));
In other words, you should treat full path (e.g., "c:\folder\filename.ext") as absolute URI.
Change imgPhoto.Source code as below
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Filter = "Image files (*.jpg)|*.jpg|All Files (*.*)|*.*";
if (dlg.ShowDialog() == true)
{
string selectedFileName = dlg.FileName;
//imgPhoto.Source = new BitmapImage(new Uri(selectedFileName, UriKind.Relative));
imgPhoto.Source = (ImageSource)new ImageSourceConverter().ConvertFromString(selectedFileName);
}
}

Dynamically add pictures to Windows forms application

I am currently trying to insert a new PictureBox and show it with its associated image.I am creating PictureBox every time i open a new image. But it doesn't show after the dialog is closed. I am also trying to store the PictureBox objects in a list.
Here is the code I have written :
OpenFileDialog FileDlg = new OpenFileDialog();
FileDlg.Filter = "Image files (*.jpg, *.jpeg, *.png, *.bmp) | *.jpg; *.jpeg; *.png, *.bmp";
if (FileDlg.ShowDialog() == DialogResult.OK)
{
PictureBox picBox = new PictureBox();
picBox.Name = "PictureBox" + m_nPictureBoxCounter.ToString();
m_picboxList.Add(picBox);
picBox.Image = Image.FromFile(FileDlg.FileName);
picBox.BringToFront();
picBox.Location = new Point(10, 10);
picBox.Size = new Size(500, 500);
picBox.Visible = true;
}
You need to add picturebox into a control, i.e.:
this.controls.add(picBox);
OpenFileDialog FileDlg = new OpenFileDialog();
FileDlg.Filter = "Image files (*.jpg, *.jpeg, *.png, *.bmp) | *.jpg; *.jpeg; *.png, *.bmp";
if (FileDlg.ShowDialog() == DialogResult.OK)
{
var picBx = new PictureBox();
picBx.ImageLocation = FileDlg.FileName;
this.Controls.Add(picBx);
}
That should put you in the right place. You can adjust position and other properties as you see fit.
Thanks to Btc Sources i figured out the answer. I had to ad :
picBox.Parent = this;
picBox.BringToFront();

C#: Using regular expression (Regex) to duplicate a specific character in a string

Anyone know how to use regex to duplicate a specific character in a string?
I have a path that is entered like this:
C:/Example/example
I would like to use regex (or any other method) to display it like this:
C://Example//example
Is it possible?
This is where I'm getting the file path
private void btnSearchImage_Click_1(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string filenName = ofd.FileName;
pictureBox1.Image = new Bitmap(filenName);
string path = filenName;
txtimgPath.Text = path;
}
}
Thanks
How about this without RegEx?
var text = "C:/Example/example";
string outputValue = text.Replace("/","//"); //returns "C://Example//example"
This should work:
private void btnSearchImage_Click_1(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string filenName = ofd.FileName;
pictureBox1.Image = new Bitmap(filenName);
string path = filenName.Replace("/", "//");
txtimgPath.Text = path;
}
}

How To Save Image in My WPF Application

In My WPF application i am not able to save image inside my application in snap folder. Below is the code i am using.
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (ofd.ShowDialog() == DialogResult.OK)
{
string filepath = ofd.FileName;
File.Copy(ofd.FileName, Application.StartupPath + "\\snaps\\" + ofd.SafeFileName,true);
photoTextBox.Text= ofd.SafeFileName;
pictureBox1.Image = Image.FromFile(ofd.FileName);
}
Code to open the file browser
string filepath;
//browse Button
private void button4_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Multiselect = false;
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
bool? result = open.ShowDialog();
if (result == true)
{
filepath = open.FileName; // Stores Original Path in Textbox
ImageSource imgsource = new BitmapImage(new Uri(filepath)); // Just show The File In Image when we browse It
Clientimg.Source = imgsource;
}
}
And below is the code used for saving the file
private static String GetDestinationPath(string filename, string foldername)
{
String appStartPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
appStartPath = String.Format(appStartPath + "\\{0}\\" + filename, foldername);
return appStartPath;
}
How to use it
string name = System.IO.Path.GetFileName(filepath);
string destinationPath = GetDestinationPath(name,"YourFolderName");
File.Copy(filepath, destinationPath, true);

Change the Display size of Image on Form resizing

I want that when i resize the form, Image Display resize with form size automatically
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp; *.png";
if(openFileDialog1.ShowDialog() == DialogResult.OK){
pictureBox1.ImageLocation = openFileDialog1.FileName;
//pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
}
}
Suggest me some code please
try
pictureBox1.Dock = DockStyle.Fill;
and
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
Edit
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;

Categories