Skip to content

Instantly share code, notes, and snippets.

@einnar82
Last active August 20, 2024 08:07
Show Gist options
  • Select an option

  • Save einnar82/4821d0f4753413f15d1c859e88cce81e to your computer and use it in GitHub Desktop.

Select an option

Save einnar82/4821d0f4753413f15d1c859e88cce81e to your computer and use it in GitHub Desktop.

Revisions

  1. einnar82 revised this gist Aug 20, 2024. 1 changed file with 36 additions and 33 deletions.
    69 changes: 36 additions & 33 deletions image.cs
    Original file line number Diff line number Diff line change
    @@ -1,54 +1,57 @@
    using System;
    using MySql.Data.MySqlClient;
    using System.Drawing;
    using System.IO;
    using System.Windows.Forms;
    using MySql.Data.MySqlClient; // Ensure you have the MySQL Connector for .NET

    class Program
    public class MainForm : Form
    {
    static void Main(string[] args)
    private PictureBox pictureBox;

    public MainForm()
    {
    InitializeComponents();
    }

    private void InitializeComponents()
    {
    string connectionString = "YourMySqlConnectionString";
    string query = "SELECT `Logo` FROM tblmunicipality WHERE id = @id";
    int municipalityId = 1; // Replace with the actual ID you want to retrieve
    pictureBox = new PictureBox
    {
    Dock = DockStyle.Fill,
    SizeMode = PictureBoxSizeMode.Zoom
    };

    Controls.Add(pictureBox);
    LoadImageFromDatabase();
    }

    byte[] imageData = null;
    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))
    {
    connection.Open();
    MySqlCommand command = new MySqlCommand(query, connection);
    command.Parameters.AddWithValue("@id", municipalityId);
    command.Parameters.AddWithValue("@id", 1); // replace with your image ID

    using (MySqlDataReader reader = command.ExecuteReader())
    {
    if (reader.Read())
    {
    // Assuming you're retrieving the 'Logo' column
    imageData = reader["Logo"] as byte[];
    }
    }
    }
    connection.Open();
    byte[] imageData = command.ExecuteScalar() as byte[];
    connection.Close();

    if (imageData != null && imageData.Length > 0)
    {
    try
    if (imageData != null)
    {
    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);
    pictureBox.Image = Image.FromStream(ms);
    }
    }
    catch (Exception ex)
    {
    Console.WriteLine("Error creating image: " + ex.Message);
    }
    }
    else
    {
    Console.WriteLine("Image not found or invalid image data.");
    }
    }

    [STAThread]
    static void Main()
    {
    Application.Run(new MainForm());
    }
    }
  2. einnar82 revised this gist Aug 20, 2024. 1 changed file with 35 additions and 61 deletions.
    96 changes: 35 additions & 61 deletions image.cs
    Original file line number Diff line number Diff line change
    @@ -1,80 +1,54 @@
    using System;
    using System.Data;
    using MySql.Data.MySqlClient;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using MySql.Data.MySqlClient;

    namespace MunMapImageFetcher
    class Program
    {
    class Program
    static void Main(string[] args)
    {
    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))
    {
    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;
    connection.Open();
    MySqlCommand command = new MySqlCommand(query, connection);
    command.Parameters.AddWithValue("@id", municipalityId);

    try
    using (MySqlDataReader reader = command.ExecuteReader())
    {
    using (MySqlConnection connection = new MySqlConnection(connectionString))
    if (reader.Read())
    {
    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"];
    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");
    }
    // Assuming you're retrieving the 'Logo' column
    imageData = reader["Logo"] as byte[];
    }
    }
    }

    image.Save(Path.Combine(projectRoot, imageName), format);
    Console.WriteLine("Image saved successfully to: " + Path.Combine(projectRoot, imageName));
    }
    }
    }
    }
    else
    {
    Console.WriteLine("No data found for id=1.");
    }
    }
    }
    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("An error occurred: " + ex.Message);
    Console.WriteLine("Error creating image: " + ex.Message);
    }
    }
    else
    {
    Console.WriteLine("Image not found or invalid image data.");
    }
    }
    }
  3. einnar82 revised this gist Aug 20, 2024. 1 changed file with 33 additions and 4 deletions.
    37 changes: 33 additions & 4 deletions image.cs
    Original 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"];
    string imageName = "MunMapImage.jpg"; // Or use a dynamic name
    using (MemoryStream ms = new MemoryStream(imageBytes))
    {
    using (Image image = Image.FromStream(ms))
    {
    string imageName;
    ImageFormat format;

    File.WriteAllBytes(Path.Combine(projectRoot, imageName), imageBytes);
    Console.WriteLine("Image saved successfully to: " + Path.Combine(projectRoot, imageName));
    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)
    }
    }
    }
    }
    }
  4. einnar82 revised this gist Aug 20, 2024. 1 changed file with 36 additions and 38 deletions.
    74 changes: 36 additions & 38 deletions image.cs
    Original file line number Diff line number Diff line change
    @@ -1,53 +1,51 @@
    using System;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Data;
    using System.IO;
    using MySql.Data.MySqlClient;

    class Program
    namespace MunMapImageFetcher
    {
    static void Main(string[] args)
    class Program
    {
    // 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))
    static void Main(string[] args)
    {
    connection.Open();
    SqlCommand command = new SqlCommand(query, connection);
    command.Parameters.AddWithValue("@Id", imageId);
    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;

    using (SqlDataReader reader = command.ExecuteReader())
    try
    {
    if (reader.Read())
    using (MySqlConnection connection = new MySqlConnection(connectionString))
    {
    imageData = (byte[])reader["ImageColumn"];
    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.");
    }
    }
    }
    }
    }
    }

    if (imageData != null)
    {
    // Convert the byte array to an Image object
    using (MemoryStream ms = new MemoryStream(imageData))
    catch (Exception ex)
    {
    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);
    Console.WriteLine("An error occurred: " + ex.Message);
    }
    }
    else
    {
    Console.WriteLine("Image not found.");
    }
    }
    }
    }
  5. einnar82 created this gist Aug 20, 2024.
    53 changes: 53 additions & 0 deletions image.cs
    Original 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.");
    }
    }
    }