C# - Find and convert Image path to base64 - c#

I'm working on an assignment and I'm trying to either convert an image to base64 but after trying some examples on here I get an error message saying the bytes can't exceed infinity, and someone in class suggested I should export the image path instead.
below is the code I have to open the image
private void button1_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "PNG Files (.png)|*.png|JPEG Files (.jpg)|*.jpg";
openFileDialog1.FilterIndex = 2;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
Bitmap loadedImage = new Bitmap(openFileDialog1.FileName);
setImage.Size = loadedImage.Size;
setImage.BackgroundImage = loadedImage;
imageWidth.Text = loadedImage.Size.Width.ToString();
imageHeight.Text = loadedImage.Size.Height.ToString();
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
// Insert code to read the stream here.
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
after opening the Image I need to be able to export the dimensions of the image to XML but I also want to export the image path there as well.
Below is the code I'm using to export as you can see I haven't done the image export as I'm unsure what to do there
private void exportButton_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
DataTable xyPos = new DataTable();
xyPos.TableName = "Position";
xyPos.Columns.Add("X");
xyPos.Columns.Add("Y");
ds.Tables.Add(xyPos);
DataTable whDimen = new DataTable();
whDimen.TableName = "Dimensions";
whDimen.Columns.Add("Width");
whDimen.Columns.Add("Height");
ds.Tables.Add(whDimen);
DataTable loadImage = new DataTable();
loadImage.TableName = "LoadedImage";
loadImage.Columns.Add("Image");
ds.Tables.Add(loadImage);
DataRow posRow = ds.Tables["Position"].NewRow();
posRow["X"] = Xcoord.Text;
posRow["Y"] = Ycoord.Text;
ds.Tables["Position"].Rows.Add(posRow);
DataRow dimenRow = ds.Tables["Dimensions"].NewRow();
dimenRow["Width"] = imageWidth.Text;
dimenRow["Height"] = imageHeight.Text;
ds.Tables["Dimensions"].Rows.Add(dimenRow);
//Export Image
DataRow imageRow = ds.Tables["LoadedImage"].NewRow();
imageRow["Image"] = setImage.BackgroundImage;
//ImageConverter imgConvert = new ImageConverter();
//byte[] imgByte = (byte[])imgConvert.ConvertTo(imageRow["Image"], typeof(byte[]));
//Convert.ToBase64String(imgByte);
//Convert.ToBase64String((byte[])imageRow["Image"]);
ds.Tables["LoadedImage"].Rows.Add(imageArray);
ds.WriteXml("XMLFile1.xml");
}

Related

C# and Ghostscript.net results in error (no picture)

I tried to create a little program to convert PDF to a TIF file using ghostscript but unfortunately it results in an error ("null"). Can't figure out why it's failing:
void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "PDF Files|*.pdf";
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
strfilename = openFileDialog1.FileName;
}
}
void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog targetfolder = new FolderBrowserDialog();
if (targetfolder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
folder = targetfolder.SelectedPath;
}
}
void button3_Click(object sender, EventArgs e)
{
const string DLL_64BITS = "gsdll64.dll";
string NomeGhostscriptDLL;
NomeGhostscriptDLL = DLL_64BITS;
GhostscriptVersionInfo gvi = new GhostscriptVersionInfo(NomeGhostscriptDLL);
///var xDpi = 300;
var yDpi = 300;
using (var rasterizer = new GhostscriptRasterizer())
{
byte[] buffer = File.ReadAllBytes(strfilename);
MemoryStream ms = new MemoryStream(buffer);
rasterizer.Open(ms, gvi, true);
int PdfPages = rasterizer.PageCount;
for (int pageNumber = 1; pageNumber < rasterizer.PageCount; pageNumber++)
{
string outputTIFPath = Path.Combine(folder, "00" + pageNumber.ToString() + ".tiff");
Image pdf2TIF = rasterizer.GetPage(yDpi, pageNumber);
MessageBox.Show(outputTIFPath);
pdf2TIF.Save(outputTIFPath, ImageFormat.Tiff);
}
rasterizer.Close();
}
}
The error looks like this
Can anyone help me to sort this out?
try adding this
MyPlaceHolder.Controls.Add(pd2TIF);
below:
Image pdf2TIF = rasterizer.GetPage(yDpi, pageNumber);
I just read that on a different thread. im not 100% sure if it works

I'm Using C# MySql, while storing image path in Gridview it neglect the \ from path, due to which i'm getting error while retrieving image from path

// Browse the image and save in Picturebox and path saved in label
private void BtnAdd_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog() {Filter = "JPEG|*.jpg", ValidateNames = true, Multiselect = false})
{
if(ofd.ShowDialog() == DialogResult.OK)
{
filename = ofd.FileName.ToString();
LblFileName.Text = filename;
pictureBox1.Image = Image.FromFile(filename);
}
}
}
// This convert image to Binary
byte[] ConvertImageToBinary(Image Img)
{
using (MemoryStream ms = new MemoryStream())
{
Img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
}
// Here i'm filling data into gridview, but automatically path C:\User\Profile.jpg Becomes CuUserProfile.jpg , actually it is neglecting \ while storing path in gridview and that is why i'm getting error while retrieving image from that path, so kindly help with the solution
public void FillDataGridView()
{
DgvEmpReg.Rows.Clear();
foreach (DataRow item in dt.Rows)
{
int n = DgvEmpReg.Rows.Add();
DgvEmpReg.Rows[n].Cells[0].Value = item[0].ToString();
DgvEmpReg.Rows[n].Cells[1].Value = item[1].ToString();
conn.Close();
}
}

C#: Choose XML file to open in a DataGridView for DataBinding

I would like to load a DataGridView from an XML file.
I put the 'load' code in a Button event like this:
private void metroButton13_Click(object sender, EventArgs e)
{
// load
DataSet dataSet = new DataSet();
dataSet.ReadXml(#"C:\temp\xml.xml");
dataGridView1.DataSource = dataSet.Tables[0];
}
And it loads correctly what I want using a const UniCode-String.
What I need now is a PopUp Window in which I can choose the file to be bound to the DataSource instead of the const "C:\temp\xml.xml" string.
Yes I know there is a lot of topic I try, but so far I'm unable to do this in my project.
You can use OpenFileDialog to select the file and pass this to ReadXml. Something like the below lines would solve your problem.
private void metroButton13_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog();
int size =0;
string file = string.empty;
if (result == DialogResult.OK) // Test result.
{
string file = openFileDialog1.FileName;
try
{
string text = File.ReadAllText(file);
size = text.Length;
}
catch (IOException)
{
}
}
if(size >0)
{
DataSet dataSet = new DataSet();
dataSet.ReadXml(file);
dataGridView1.DataSource = dataSet.Tables[0];
}
else
{
msgbox ("blank file");
}
}
DataSet dataSet = new DataSet();
OpenFileDialog sfd = new OpenFileDialog();
sfd.Filter = "XML|*.xml";
if (sfd.ShowDialog() == DialogResult.OK)
{
string file = sfd.FileName;
try
{
dt.ReadXml(file);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
Actually this code solve my problem but you give me something to think just. Anyway thank you!

Can't open video picturebox using Opencv library

I can't open video in picturebox My code is:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.FileName = "*.*";
if (open.ShowDialog() == DialogResult.OK)
{
isAnyText = false;
// string text = open.FileName;
// MessageBox.Show("resimin yolu :" + text);
/// CvVideoWriter video = new CvVideoWriter();
// videoSource.NewFrame += new NewFrameEventHan
CvCapture capture = new CvCapture(open.FileName);
pictureBox1.Image = new Bitmap(capture);
// pictureBox1.Image = capture;// new Bitmap(pictureBox1.Image);
//pictureBox1.Capture = new Bitmap(open.FileName);
// pictureBox1.Image.
string plaka = ft.GetImageText(open.FileName);
var img = pictureBox1.Image;
}
foreach (var s in ft.textList)
listBox1.Items.Add(s);
}
I have a problem here
pictureBox1.Image = new Bitmap(capture);
At the same time I try this:
pictureBox1.Capture = new Bitmap(open.FileName);
and I try lots of things but I cant do it
Any advance?

PDFBox throws exception: NullReference Exception

pdfbox issue
I used pdfbox to extract text from PDF to my richtextbox.
I don't know what's the problem but there are PDF that are good but there are PDF that throws an exception, the exception is:
Object reference not set to an instance of an object.
Here's my code:
using org.pdfbox.pdmodel;
using org.pdfbox.util;
private void pdfButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFD = new OpenFileDialog();
openFD.FileName = "";
openFD.InitialDirectory = "C:\\";
openFD.Filter = "All PDF Files|*.PDF";
openFD.Title = "Browse all PDF files";
if (openFD.ShowDialog() == DialogResult.OK)
{
try
{
pdf_filename = Path.GetFileNameWithoutExtension(openFD.Filename);
PDDocument pdfFile = PDDocument.load(openFD.Filename);
PDFTextStripper pdfStripper = new PDFTextStripper();
richtextBox1.Text = pdfStripper.getText(pdfFile);
textBox1.Text = Path.GetFileName(openFD.Filename);
}
catch (Exception error)
{
MessageBox.Show(error.ToString());
}
}
}
I fixed the issue using iTextSharp. This was advised by my co-worker, I changed the PDFBox by iTextSharp.
If someone will have the same issue as me here's the working code:
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
private void pdfButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFD = new OpenFileDialog();
openFD.FileName = "";
openFD.InitialDirectory = "C:\\";
openFD.Filter = "All PDF Files|*.PDF";
openFD.Title = "Browse all PDF files";
if (openFD.ShowDialog() == DialogResult.OK)
{
try
{
pdf_filename = Path.GetFileNameWithoutExtension(openFD.Filename);
richtextBox1.Text = ReadPdf(openFD.FileName);
textBox1.Text = Path.GetFileName(openFD.Filename);
}
catch (Exception error)
{
MessageBox.Show(error.ToString());
}
}
}
private string ReadPdf(string filename)
{
if (!File.Exists(filename)) return string.Empty;
PdfReader reader = new PdfReader(filename);
string text = string.Empty;
for (int page = 1; page <= reader.NumberOfPages; page++)
{
text += PdfTextExtractor.GetTextFromPage(reader, page);
}
return text;
}

Categories

Resources