Upload a picture via ftp with a randomized name C# - c#

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

Related

WPF C# Read a txt file in Textbox who is selected in combobox

Maybe this is an old question, but I canĀ“t find a solution fpr my project. Let me explain:
I have 3 Textboxes, who I can fill with values.
1 Button who save the values of the textboxes in a *.txt file. The Name of the file is the value of the first textboxes.
Then there is a ComboBox where I can select the txt files.
But I cant find the right code, when I selected a file, I want to see the 3 values(3 different lines) of the file into the Textboxes. Can someone help me, I will show you the Codes maybe its easier to understand.
the first code to save the values (tb1-3 = Textbox) in the txt file:
private void savebutton_click(object sender, RoutedEventArgs e)
{
string folder = #"C:\Users\...\...\...\Debug\";
string filename = tb1.Text + ", " + tb2.Text;
string writerfile = folder + filename;
using (StreamWriter writer = new StreamWriter(writerfile))
{
writer.WriteLine(this.tb1.Text);
writer.WriteLine(this.tb2.Text);
writer.WriteLine(this.tb3.Text);
}
}
The second code is to show the txt files in the combobox(combo1):
private void comboboxloaded(object sender, RoutedEventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(#"C:\Users\...\...\...\Debug");
FileInfo[] Files = dinfo.GetFiles("*.txt");
foreach (FileInfo file in Files)
{
combo1.Items.Add(file);
}
}
And know... I need help...
private void comboboxvalueselect(object sender, SelectionChangedEventArgs e)
{
// Maybe?????
string fileName = combo1.SelectedItem.ToString();
string filePath = System.IO.Path.Combine(#"C:\Users\...\...\...\Debug" + fileName + "*.txt");
// and something with StreamReader??????
}
If I understand the question it could be as simple as using File.ReadAllLines
Opens a text file, reads all lines of the file into a string array,
and then closes the file.
private void comboboxvalueselect(object sender, SelectionChangedEventArgs e)
{
const string somePath = #"C:\Users\...\...\...\Debug";
var fileName = combo1.SelectedItem.ToString();
var filePath = Path.Combine(somePath , fileName);
// where you need those lines to go
someTextBox.Text = File.ReadAllLines(filePath);
}

set label1.text as image name & show image c#

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

Saving image from "pictureBox1" to a file with a click on a button in C#

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

Flickering image after loading in winforms at timer tick event

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.

OCR reading using C#

I have a project which is to read character in a captured image but I'm stuck at the button which is to scan image. I ended up tesseract dll in c#, but I don't know how can I code it. I'm a newbie to this programming.
private void Browse_Click(object sender, EventArgs e)
{
//FileInfo fi = new FileInfo(string.Format(#"C:\Documents and Settings\JOrce0201610\My Documents\Visual Studio 2005\Projects\OCR Reader\{0}", imageName));
OpenFileDialog fi = new OpenFileDialog();
fi.InitialDirectory = #"C:\\Documents and Settings\JOrce0201610\My Documents\Visual Studio 2005\Projects\OCR Reader\Card";
fi.Filter = "BMP Image|*.bmp";
fi.FilterIndex = 2;
fi.RestoreDirectory = true;
if (fi.ShowDialog() == DialogResult.OK)
{
//image file path
textBox1.Text = fi.FileName;
//display image in picture box
pictureBox1.Image = new Bitmap(fi.FileName);
}
}
private void Scan_Click(object sender, EventArgs e)
{
Bitmap temp = source.Clone() as Bitmap; //Clone image to keep original image
FiltersSequence seq = new FiltersSequence();
seq.Add(Grayscale.CommonAlgorithms.BT709); //First add GrayScaling filter
seq.Add(new OtsuThreshold()); //Then add binarization(thresholding) filter
temp = seq.Apply(source); // Apply filters on source image
If you are a 'newbie' to programming, OCR is not the best place to start. The best I can suggest is that you use a webservice or existing library that can do this for you.
Microsoft has project Hawaii, Hawaii has an OCR service which is quite easy to use.

Categories

Resources