Last active
August 20, 2024 08:07
-
-
Save einnar82/4821d0f4753413f15d1c859e88cce81e to your computer and use it in GitHub Desktop.
Revisions
-
einnar82 revised this gist
Aug 20, 2024 . 1 changed file with 36 additions and 33 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,54 +1,57 @@ 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()); } } -
einnar82 revised this gist
Aug 20, 2024 . 1 changed file with 35 additions and 61 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,80 +1,54 @@ using System; using MySql.Data.MySqlClient; using System.Drawing; using System.IO; class Program { static void Main(string[] args) { string connectionString = "YourMySqlConnectionString"; string query = "SELECT `Logo` FROM tblmunicipality WHERE id = @id"; int municipalityId = 1; // Replace with the actual ID you want to retrieve byte[] imageData = null; using (MySqlConnection connection = new MySqlConnection(connectionString)) { connection.Open(); MySqlCommand command = new MySqlCommand(query, connection); command.Parameters.AddWithValue("@id", municipalityId); using (MySqlDataReader reader = command.ExecuteReader()) { if (reader.Read()) { // Assuming you're retrieving the 'Logo' column imageData = reader["Logo"] as byte[]; } } } if (imageData != null && imageData.Length > 0) { try { using (MemoryStream ms = new MemoryStream(imageData)) { Image image = Image.FromStream(ms); string filePath = "outputLogo.png"; image.Save(filePath, System.Drawing.Imaging.ImageFormat.Png); Console.WriteLine("Image saved to " + filePath); } } catch (Exception ex) { Console.WriteLine("Error creating image: " + ex.Message); } } else { Console.WriteLine("Image not found or invalid image data."); } } } -
einnar82 revised this gist
Aug 20, 2024 . 1 changed file with 33 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,7 @@ using System; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.IO; using MySql.Data.MySqlClient; @@ -28,10 +30,37 @@ static void Main(string[] args) while (reader.Read()) { byte[] imageBytes = (byte[])reader["MunMap"]; using (MemoryStream ms = new MemoryStream(imageBytes)) { using (Image image = Image.FromStream(ms)) { string imageName; ImageFormat format; if (ImageFormat.Jpeg.Equals(image.RawFormat)) { imageName = "MunMapImage.jpg"; format = ImageFormat.Jpeg; } else if (ImageFormat.Png.Equals(image.RawFormat)) { imageName = "MunMapImage.png"; format = ImageFormat.Png; } else if (ImageFormat.Bmp.Equals(image.RawFormat)) { imageName = "MunMapImage.bmp"; format = ImageFormat.Bmp; } else { throw new NotSupportedException("Unsupported image format"); } image.Save(Path.Combine(projectRoot, imageName), format); Console.WriteLine("Image saved successfully to: " + Path.Combine(projectRoot, imageName)); } } } } else @@ -48,4 +77,4 @@ static void Main(string[] args) } } } } -
einnar82 revised this gist
Aug 20, 2024 . 1 changed file with 36 additions and 38 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,53 +1,51 @@ using System; using System.Data; using System.IO; using MySql.Data.MySqlClient; namespace MunMapImageFetcher { class Program { static void Main(string[] args) { string connectionString = "server=your_server;database=your_database;uid=your_username;pwd=your_password;"; string sqlQuery = "SELECT MunMap FROM tblmunicipality WHERE id=1"; string projectRoot = AppDomain.CurrentDomain.BaseDirectory; try { using (MySqlConnection connection = new MySqlConnection(connectionString)) { connection.Open(); using (MySqlCommand command = new MySqlCommand(sqlQuery, connection)) { using (MySqlDataReader reader = command.ExecuteReader()) { if (reader.HasRows) { while (reader.Read()) { byte[] imageBytes = (byte[])reader["MunMap"]; string imageName = "MunMapImage.jpg"; // Or use a dynamic name File.WriteAllBytes(Path.Combine(projectRoot, imageName), imageBytes); Console.WriteLine("Image saved successfully to: " + Path.Combine(projectRoot, imageName)); } } else { Console.WriteLine("No data found for id=1."); } } } } } catch (Exception ex) { Console.WriteLine("An error occurred: " + ex.Message); } } } } -
einnar82 created this gist
Aug 20, 2024 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,53 @@ using System; using System.Data.SqlClient; using System.Drawing; using System.IO; class Program { static void Main(string[] args) { // Replace with your connection string string connectionString = "YourConnectionString"; // Replace with your query and parameters string query = "SELECT ImageColumn FROM YourTable WHERE Id = @Id"; int imageId = 1; // Replace with the ID of the image you want to retrieve byte[] imageData = null; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command = new SqlCommand(query, connection); command.Parameters.AddWithValue("@Id", imageId); using (SqlDataReader reader = command.ExecuteReader()) { if (reader.Read()) { imageData = (byte[])reader["ImageColumn"]; } } } if (imageData != null) { // Convert the byte array to an Image object using (MemoryStream ms = new MemoryStream(imageData)) { Image image = Image.FromStream(ms); // Save the image to a file string filePath = "outputImage.png"; image.Save(filePath, System.Drawing.Imaging.ImageFormat.Png); Console.WriteLine("Image saved to " + filePath); } } else { Console.WriteLine("Image not found."); } } }