C
C#2y ago
Hiccup

❔ Decoded bitmap images aren't set in the photoboxes correctly

private void BTN_Save_Click(object sender, EventArgs e)
{
string DBpath = @"Data Source=.\StudentDB.db;Version=3;";
Bitmap[] PictureBoxesBitmaps = {FirstPictureBitmap, SecondPictureBitmap, ThirdPictureBitmap };
using SQLiteConnection connection = new(DBpath);
using SQLiteCommand cmd = new(DBpath, connection);
connection.Open();
cmd.CommandText = @"DELETE FROM PictureBoxes";
cmd.ExecuteNonQuery();
foreach (Bitmap bitmap in PictureBoxesBitmaps)
{
System.IO.MemoryStream ms = new();
bitmap?.Save(ms, ImageFormat.Png);
byte[] BitmapByteArray = ms.ToArray();
var PictureBox64 = Convert.ToBase64String(BitmapByteArray);
cmd.CommandText = @"INSERT INTO PictureBoxes(Encoded) VALUES('" + PictureBox64 + "')";
cmd.ExecuteNonQuery();
}
connection.Close();
}
private void BTN_Save_Click(object sender, EventArgs e)
{
string DBpath = @"Data Source=.\StudentDB.db;Version=3;";
Bitmap[] PictureBoxesBitmaps = {FirstPictureBitmap, SecondPictureBitmap, ThirdPictureBitmap };
using SQLiteConnection connection = new(DBpath);
using SQLiteCommand cmd = new(DBpath, connection);
connection.Open();
cmd.CommandText = @"DELETE FROM PictureBoxes";
cmd.ExecuteNonQuery();
foreach (Bitmap bitmap in PictureBoxesBitmaps)
{
System.IO.MemoryStream ms = new();
bitmap?.Save(ms, ImageFormat.Png);
byte[] BitmapByteArray = ms.ToArray();
var PictureBox64 = Convert.ToBase64String(BitmapByteArray);
cmd.CommandText = @"INSERT INTO PictureBoxes(Encoded) VALUES('" + PictureBox64 + "')";
cmd.ExecuteNonQuery();
}
connection.Close();
}
I'm using this first code to encode three bitmap images into a base64 string, then I'm storing them in a SQLite database.
2 Replies
Hiccup
HiccupOP2y ago
public Form1()
{
InitializeComponent();
RefreshData();
using SQLiteConnection PicCheckerConnection = new(DBpath);
using var cmd = new SQLiteCommand(DBpath, PicCheckerConnection);
PicCheckerConnection.Open();
for (int i = 0; i <= 4; i++)
{
int PhotoSelect = i + 1;
cmd.CommandText = "SELECT * FROM PictureBoxes ORDER BY Encoded ASC LIMIT " + PhotoSelect + "," + PhotoSelect;
cmd.ExecuteNonQuery();
using (SQLiteDataReader read = cmd.ExecuteReader())
{
while (read.Read())
{
string Base64Image = read.GetString(0);
byte[] bytes = Convert.FromBase64String(Base64Image);
Stream stream = new MemoryStream(bytes);
if (i == 1) { pictureBox1.Image = Image.FromStream(stream);}
if (i == 2) { pictureBox2.Image = Image.FromStream(stream);}
if (i == 3) { pictureBox3.Image = Image.FromStream(stream);}
}
read.Close();
}
}
public Form1()
{
InitializeComponent();
RefreshData();
using SQLiteConnection PicCheckerConnection = new(DBpath);
using var cmd = new SQLiteCommand(DBpath, PicCheckerConnection);
PicCheckerConnection.Open();
for (int i = 0; i <= 4; i++)
{
int PhotoSelect = i + 1;
cmd.CommandText = "SELECT * FROM PictureBoxes ORDER BY Encoded ASC LIMIT " + PhotoSelect + "," + PhotoSelect;
cmd.ExecuteNonQuery();
using (SQLiteDataReader read = cmd.ExecuteReader())
{
while (read.Read())
{
string Base64Image = read.GetString(0);
byte[] bytes = Convert.FromBase64String(Base64Image);
Stream stream = new MemoryStream(bytes);
if (i == 1) { pictureBox1.Image = Image.FromStream(stream);}
if (i == 2) { pictureBox2.Image = Image.FromStream(stream);}
if (i == 3) { pictureBox3.Image = Image.FromStream(stream);}
}
read.Close();
}
}
Then I wrote this code where it selects all three base64 strings from the SQLite database, decodes them and puts them in their pictureboxes respectively, the first base64 string --> the first picture box, second base64 string --> second picture box, and third base64 string --> third picture box. However, when the program decodes the base64 strings and applies the images to the pictureboxes, the order is messed up. The second decoded image is set in the first picturebox, and the third decoded image is set in the second picturebox and the first decoded image is not set in any of the pictureboxes. I have tried debugging and changing the value of i, but sadly they did not help.
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server