Last active
August 20, 2024 08:07
-
-
Save einnar82/4821d0f4753413f15d1c859e88cce81e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using System.IO; | |
using System.Windows.Forms; | |
using MySql.Data.MySqlClient; // Ensure you have the MySQL Connector for .NET | |
public class MainForm : Form | |
{ | |
private PictureBox pictureBox; | |
public MainForm() | |
{ | |
InitializeComponents(); | |
} | |
private void InitializeComponents() | |
{ | |
pictureBox = new PictureBox | |
{ | |
Dock = DockStyle.Fill, | |
SizeMode = PictureBoxSizeMode.Zoom | |
}; | |
Controls.Add(pictureBox); | |
LoadImageFromDatabase(); | |
} | |
private void LoadImageFromDatabase() | |
{ | |
string connectionString = "your_mysql_connection_string_here"; | |
string query = "SELECT image_data FROM your_table WHERE id = @id"; | |
using (MySqlConnection connection = new MySqlConnection(connectionString)) | |
{ | |
MySqlCommand command = new MySqlCommand(query, connection); | |
command.Parameters.AddWithValue("@id", 1); // replace with your image ID | |
connection.Open(); | |
byte[] imageData = command.ExecuteScalar() as byte[]; | |
connection.Close(); | |
if (imageData != null) | |
{ | |
using (MemoryStream ms = new MemoryStream(imageData)) | |
{ | |
pictureBox.Image = Image.FromStream(ms); | |
} | |
} | |
} | |
} | |
[STAThread] | |
static void Main() | |
{ | |
Application.Run(new MainForm()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment