Save image and rename without SQL

2

I'm having difficulty saving multiple images without the use of database. The text data saved in txt, but I can not save images. I have already read the images.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
        {
            pastaLabel.Text = folderBrowserDialog1.SelectedPath;
            string[] arquivos = Directory.GetFiles(pastaLabel.Text, "*.jpg");

            foreach (var arquivo in arquivos)
            {
                ImagensListBox.Items.Add(arquivo);
            }
        }
    }

    string arquivo;
    private void ImagensListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        arquivo = ImagensListBox.SelectedItem.ToString();
        imagemPictureBox.ImageLocation = arquivo;
    }

    private void salvarButton_Click(object sender, EventArgs e)
    { 
        ? ? ?
    }
    
asked by anonymous 22.04.2016 / 21:42

1 answer

0

You can use method a below to convert imagens to string base64 .

private string ImageToBase64(string imagePath)
{
    using (Image image = Image.FromFile(imagePath))
    {
        using (MemoryStream m = new MemoryStream())
        {
            image.Save(m, image.RawFormat);
            byte[] imageBytes = m.ToArray();

            // Convert byte[] to Base64 String
            string base64String = Convert.ToBase64String(imageBytes);
            return base64String;
        }
    }
}

And to convert string base64 to imagem again:

private Image Base64ToImage(string base64)
{
    var bytes = Convert.FromBase64String(base64);
    Image image = null;
    using (var stream= new MemoryStream(bytes))
    {
        image = Image.FromStream(stream);

    }

    return image;

}
    
24.04.2016 / 02:53