」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 如何在 C# 中從 MySQL 資料庫載入和顯示映像?

如何在 C# 中從 MySQL 資料庫載入和顯示映像?

發佈於2024-11-08
瀏覽:904

How to Load and Display Images from a MySQL Database in C#?

如何從MySQL 資料庫擷取與顯示影像

如何從MySQL 資料庫擷取與顯示影像

從MySQL 資料庫擷取影像到PictureBox 控制項中需要採用具有正確位元組的特定方法數組處理。以下步驟示範了這個過程:

    將映像插入 MySQL 資料庫
  1. 使用 MySql.Data.MySqlClient 庫進行 MySQL 資料庫連接。
  2. 將映像從檔案轉換為位元組數組: byte[] bytes = File.ReadAllBytes(ofd.FileName);

    cmd.Parameters.AddWithValue("@image", bytes);
    cmd.ExecuteNonQuery();
    建立一個MySqlCommand 並使用參數將位元組數組插入資料庫:
cmd.Parameters.AddWithValue("@image", bytes); cmd.ExecuteNonQuery();

    從MySQL資料庫擷取影像
  1. 一個byte[]陣列用於儲存檢索到的影像: byte[] ImageByte = new byte[0];

    cmd.Parameters.AddWithValue("@image", bytes);
    cmd.ExecuteNonQuery();
    使用參數執行查詢以檢索映像:
  2. cmd.Parameters.AddWithValue("@id", Properties .Settings. Default.idImg); MySqlDataReader 行; row = cmd.ExecuteReader();

    cmd.Parameters.AddWithValue("@image", bytes);
    cmd.ExecuteNonQuery();
    從row讀取影像位元組陣列:
while (row.Read()) { ImageByte = (Byte[])(行["影像"]); }

    將位元組陣列轉換為映像並顯示
  1. cmd.Parameters.AddWithValue("@image", bytes);
    cmd.ExecuteNonQuery();
    使用Helper.ByteArrayToImage 將位元組陣列轉換為圖片方法:
roundPictureBox1.Image = byteArrayToImage(ImageByte); roundPictureBox1.Refresh();

    增強
  • 考慮使用影像最佳化技術來確保映像不會導致檔案大小過大。

將原始檔案名稱儲存在資料庫中,以便於資料庫外檢索和管理。 How to Load and Display Images from a MySQL Database in C#?

最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3