-4

I have a form that pops up a file browser and upon selection should get the pathway of the selected file and then convert that image into a string. I have this code:

string sfn = "";
OpenFileDialog ofD = new OpenFileDialog();
if (ofD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    sfn = ofD.FileName;
}

string imagePathway = sfn;
string imageToText = GetImageText(imagePathway);
label1.Text = imageToText;

And then for the GetImageText function:

private string GetImageText(string path)
{
    byte[] imageByte = System.IO.File.ReadAllBytes(path);
    string convertedString = Convert.ToBase64String(imageByte);
    return convertedString;
}

No error occurs, however the label does not show the text, so I assume theres a problem with my encoding process.

Sach
  • 10,091
  • 8
  • 47
  • 84
  • So what do you see when you debug the `GetImageText()` method? – Sach Jun 20 '18 at 17:01
  • "C:\\Users\\Jackson\\Pictures\\fb.png" --Is this not the correct format for the pathway? – Higuen1966 Jun 20 '18 at 17:03
  • Does visualstudio not show the label if its too big? Its been a long time since I've used it – Higuen1966 Jun 20 '18 at 17:05
  • 2
    Possible duplicate of [Convert image path to base64 string](https://stackoverflow.com/questions/21325661/convert-image-path-to-base64-string) – Shelby115 Jun 20 '18 at 17:05
  • Uh oh.... Thats never good. ConvertedString returns as "convertedString = "iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAPtJJREFUeNrtnXm8V9P6x53qNFOGSJGU6xJdZT4uyTzkZlbmjDdz5lxTGRO5ZLwqoiJzZMiUDBGKjL/IUEmlqDQq1ff3LK04OtMe1t577bXen9fr/c99uX3PevZaz/PZw3rWGoVCYQ0AAADwi7L/A0IIlVLJUf2K..." – Higuen1966 Jun 20 '18 at 17:06
  • 1
    What exactly are you trying to do? Do you want the bits in the image in an array, or do you want to convert the _path_ to a `base64` string? – Sach Jun 20 '18 at 17:09
  • Your text maybe longer than what label can really hold. Try to write it to file to see if the converted image is completely there ! – Kaj Jun 20 '18 at 17:11

1 Answers1

0

The question it's not very clear but if you want to turn an image into a string so that you can send(like tpc/ip connection) you can just save the image as a bitmap and save it in a MemoryStream

Bitmap bmp = new Bitmap(@"Your file path");
MemoryStream mem_stream= new MemoryStream();
bmp.Save(mem_stream, System.Drawing.Imaging.ImageFormat.Bmp);

Now you have the image in the MemoryStream and you can access it as bytes or string.

An image is just an array of pixels so you can save them as a string so you can even save them using rgb values like (r11*g12*b13*r21*g22*b23*....) where 1,2,3 are the indexes of the row/column of the pixel.

*Sorry if i misunderstood the question.

Kheoss - Vlad
  • 87
  • 1
  • 10