A
A
Andrey Fomin2021-01-08 22:52:49
SQLite
Andrey Fomin, 2021-01-08 22:52:49

How to convert image from picturebox?

Hello!
There is a form and on it there is a button, when pressed, the explorer opens and through it the selected image is added to the picturebox on the form.
How to convert an image from a picturebox so that it can then be written to a blob-type database cell and how to pull it out of the database and display it back in the picturebox?
Would like an example with code

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Serafim Prozorov, 2021-01-14
@a63826ndrew

Suppose you already have code that works with the database, let it be a couple of methods: LoadImage / SaveImage, the first one takes an integer ID and returns an array of bytes, and the second one does everything exactly the opposite, in addition, there is a PictureBox pointed to by the _pictureBox1 variable , then the solution will look something like this:
1. Saving:

if (_pictureBox1.Image == null)
    return;

using var destination = new MemoryStream();
{
    _pictureBox1.Image.Save(destination, ImageFormat.Jpeg)  // здесь можно выбрать любой доступный формат
}

var id = SaveImage(destination);

2. Loading
var bytes = LoadImage(id);

if (bytes == null)
    return;

using (var source = new MemoryStream())
{
    _pictureBox1.Image = Image.FromStream(source);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question