I have a project with SQlite database. I've saved images in folder " pics " ( into debug folder) & their name in the database ( column "docpic") .I set the label10 text as image name . how can I use label.text as image name (that saved in database) and show image in a picturebox ?
label.text = image.jpg and image name=image.jpg
infect I want to use label10.text as a imagename .
label10 click event :
private void label10_Click(object sender, EventArgs e)
{
pictureBox1.Image = new Bitmap("\\scan\\" + label10.Text + ".jpg");
}
Gridview click event :
private void Grid_Click(object sender, EventArgs e)
{
i = Convert.ToInt32(DT.Rows[Grid.CurrentRowIndex]["id"]);
btnDel.Enabled = true;
btnEdit.Enabled = true;
label10.Text = DT.Rows[Grid.CurrentRowIndex]["docpic"].ToString();
}
You can try this:
picturebox.Image = Image.FromFile(#"C:\...\" + label10.text, true);
Just replace the "C:\...\" with the path to the image.
A better approach will be to use the value from the database, you don't really need to put the image as a text label:
picturebox.Image = Image.FromFile(#"C:\...\" + <value from DB>, true);
Or in your case:
picturebox.Image = Image.FromFile(#"C:\...\" + DT.Rows[Grid.CurrentRowIndex]["docpic"].ToString(), true);
Related
I want to add a image from a ComboBox, but ComboBox has got only the image name.
In a DataGridView there are two cells. Cell01 for the image name, Cell02 for the image file path.
When I selected a name form the ComboBox i want create a variable for add its correct file path form the DataGridView. What is the code for identify the correct file path for selected image name..?
"This below code will work only when adding images from same Location.. "
string imagename = cmbDataGridLink.SelectedItem.ToString();
int selectedRow = dataGridView1.CurrentCell.RowIndex;
string filepath = dataGridView1.Rows[selectedRow].Cells[2].Value.ToString();
string path = filepath + imagename;
picbComboBox.Image = Image.FromFile(path);
It looks like you've already got most of what you're looking for. Assuming that you are just trying to listen for cell selected events you could try something like setting up an event listener on the DataGridView for when the selection is changed by user and execute your code there:
//Create a custom listener for SelectionChanged on the DataGridView :
private void DataGridView1_SelectionChanged(object sender, EventArgs e)
{
string imagename = cmbDataGridLink.SelectedItem.ToString();
int selectedRow = dataGridView1.CurrentCell.RowIndex;
string filepath = dataGridView1.Rows[selectedRow].Cells[2].Value.ToString();
string path = filepath + imagename;
picbComboBox.Image = Image.FromFile(path);
}
//then add the listener to your DataGridView when Window or app loads:
DataGridView1.SelectionChanged += new EventHandler(
DataGridView1_SelectionChanged);
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview.selectionchanged?view=netframework-4.8
EDIT: after re-reading the question I am extremely confused about what you want. If you want to get the selection of the ComboBox index changed event then use this code:
//Create a custom listener for ComboBox.SelectedIndexChanged on the ComboBox :
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string imagename = cmbDataGridLink.SelectedItem.ToString();
int selectedRow = dataGridView1.CurrentCell.RowIndex;
string filepath = dataGridView1.Rows[selectedRow].Cells[2].Value.ToString();
string path = filepath + imagename;
picbComboBox.Image = Image.FromFile(path);
}
//then add the listener to your ComboBox when Window or app loads:
ComboBox1.SelectedIndexChanged +=
new System.EventHandler(ComboBox1_SelectedIndexChanged);
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.combobox.selectedindexchanged?view=netframework-4.8
As the title says, I'm trying to upload a picture from my application but I need to upload many with all different names (eg: PicJ8S23D.png) but I cannot figure it out...
The problem: I made a random string which 'would' create PicJ8S23D.png but when I try to upload it I'm unable to find the file (because I'm searching for a file that doesn't exist(because I just randomized the name.))
(I'm taking a picture in .bmp format first thats why I convert it at the bottom.)
private void button1_Click(object sender, EventArgs e)
{
System.Drawing.Image image = System.Drawing.Image.FromFile("Pic.bmp");
image.Save("Pic"+ RandomString1(5) +".png", System.Drawing.Imaging.ImageFormat.Png);
UploadImage("ftp://example.com", "uname", "pword", "pic"+ RandomString1(5) +".png");
}
All you have to do is save the name you're creating in a variable so you can use the same value twice:
var randomFilename = "pic" + RandomString(5) + ".png";
The complete solution looks like:
private void button1_Click(object sender, EventArgs e)
{
var randomFilename = "pic" + RandomString1(5) + ".png";
System.Drawing.Image image = System.Drawing.Image.FromFile("Pic.bmp");
image.Save(randomFilename, System.Drawing.Imaging.ImageFormat.Png);
UploadImage("ftp://example.com", "uname", "pword", randomFilename);
}
It seems you are trying to save an Image with Random Name First and then trying to find the same file then RandomString1(5) will give a new string everytime so what you can do is. You should make a variable to store the RandomString and then look for the same RandomString. Hope code below might help you.
private void button1_Click(object sender, EventArgs e)
{
string RndString = RandomString1(5);
System.Drawing.Image image = System.Drawing.Image.FromFile("Pic.bmp");
image.Save("Pic" + RndString + ".png", System.Drawing.Imaging.ImageFormat.Png);
UploadImage("ftp://example.com", "uname", "pword", "pic" + RndString + ".png");
}
How can I display embedded images when the listView1 SelectedIndexChanged, I need to pull the image name based on the listview item and display it on the picturebox?
My code:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
label1.Text = listView1.SelectedItems[0].SubItems[0].Text + " " +
textBox2.Text;
var myimage = "_" + listView1.SelectedItems[0].SubItems[0].Text;
// new code tryout
// When I try to do it like this Code runs fine
// but no image is displayed on picturebox
object O = Resources.ResourceManager.GetObject("myimage");
pictureBox1.Image = (Image)O;
//no image displayed and no errors
}
else
{
label1.Text = string.Empty;
}
}
The problem here is that you are confusing the string "myimage" with the content of myimage variable.
You should use the variable's content and not the string as the GetObject parameter:
object O = Resources.ResourceManager.GetObject(myimage);
Based on the fact that I don't know how you did fill your ListView, I took the idea of yours and it works fine. Here's a modified code with a screenshot of the result :
private void View_SelectedIndexChanged(object sender, EventArgs e)
{
if (View.SelectedItems.Count > 0)
{
object O = Resources.ResourceManager.GetObject(View.SelectedItems[0].Text);
PicBox.Image = (Image)O;
}
}
I found this code on here, and I want to save the image I get in my "pictureBox1" with a button like under, how can I implement these together?
I have the picture showing in the pictureBox1, I want to click a button and be able to store the picture on my PC.
private void button1_Click(object sender, EventArgs e)
This is the code to save image:
public static void SaveImageCapture(System.Drawing.Image image)
SaveFileDialog s = new SaveFileDialog();
s.FileName = "Image";// Default file name
s.DefaultExt = ".Jpg";// Default file extension
s.Filter = "Image (.jpg)|*.jpg"; // Filter files by extension
s.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
s.RestoreDirectory = true;
if (s.ShowDialog() == DialogResult.OK)
{
// Save Image
string filename = s.FileName;
(System.IO.FileStream fstream = new System.IO.FileStream(filename, System.IO.FileMode.Create))
{
image.Save(fstream, System.Drawing.Imaging.ImageFormat.Jpeg);
fstream.Close();
I am also not 100% sure i understand what you mean. but here is a 2 lines of codes, showing the simplest way of loading and saving an image into a picturebox.
// 1. Load a picture into the picturebox
PictureBox pic = new PictureBox() { Image = Image.FromFile("SomeFile.jpg") };
// 2. Save it to a file again
pic.Image.Save("SomeFilePath.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
I fixed it by doing this:
private void btnSave_Click(object sender, EventArgs e)
{
pictureBox1.Image.Save(#"C:\Temp\test.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
My question is the following:
I have a timer and each 1 ms i want to load an image and display it in a picture box in win forms
The first images work very fine however from some point the images start loading hard.
Can you please tell me how to solve this problem?
Regards and thank you for your time :)
private string path = "";
private int index = 0;
private void fileSourceToolStripMenuItem_Click(object sender, EventArgs e)
{
if (ofd.ShowDialog() == DialogResult.OK)
{
string fullPath = ofd.FileName;
string fileName = ofd.SafeFileName;
path = fullPath.Replace(fileName, "");
MessageBox.Show("Path for file source has been selected");
}
}
private const string IMAGENAME = "TestImage";
Bitmap b;
private void timer_Tick(object sender, EventArgs e)
{
try
{
string currentImage = IMAGENAME + index.ToString() + ".bmp";
index++;
b = new Bitmap(path + "\\" + currentImage);
pb.SizeMode = PictureBoxSizeMode.StretchImage;
pb.Image = b;
}
catch { };
}
private void button1_Click(object sender, EventArgs e)
{
timer.Start();
}
private void button2_Click(object sender, EventArgs e)
{
timer.Stop();
}
private void button3_Click(object sender, EventArgs e)
{
try
{
string currentImage = IMAGENAME + index.ToString() + ".bmp";
index += 1;
Bitmap b = new Bitmap(path + "\\" + currentImage);
pb.Image = b;
}
catch { };
}
private void button4_Click(object sender, EventArgs e)
{
try
{
index -= 1;
string currentImage = IMAGENAME + index.ToString() + ".bmp";
Bitmap b = new Bitmap(path + "\\" + currentImage);
pb.Image = b;
}
catch { };
}
Play with DoubleBuffered property first.
GDI+ is very slow and even when using double buffering then it's still slow.
I would suggest you to find an alternative as there isn't really a way to fix the "flickering".
However there is one thing, the way you're actually loading the images in the tick handle is pretty cpu consuming.
I would suggest you load all the required image's bytes into a collection of some sort, ex. an array.
Then simply create the bitmap from the image bytes.
Besides you're not even disposing the current image, which you should before allocating new memory for the next image.