1

1つのpictureBox、1つのtextBox、および3つのボタン(LoadFromFile、SaveToDB、およびLoadFromDB)を含む1つのWindowsフォームがあります。

App.configでのLINQtoSQL接続と、次の形式の次のコードの使用:

private void btnLoadFromFile_Click(object sender, EventArgs e)
{
// open file dialog
OpenFileDialog open = new OpenFileDialog();
// image filters
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.png)|*.jpg; *.jpeg; *.gif; *.bmp; *.png";
if (open.ShowDialog() == DialogResult.OK)
{
// display image in picture box
picBox.Image = new Bitmap(open.FileName);
// image file path
textBox1.Text = open.FileName;
}
}

Pictureとそのパスをロードしてフォームを作成できます。

[BLOBData]次に、画像をテーブルで指定された画像タイプフィールドに保存する必要がありますtblBLOB (BLOBID, BLOBData)PictureBoxでは、画像を画像タイプに変換するコードと、画像タイプを画像に変換してコントロールに表示するコードは何ですか?

4

1 に答える 1

3

データベースにblobByte Arrayとして保存します。いくつかの便利な変換方法:

public static byte[] ImageToByteArray(Image imageIn)
{
    var ms = new MemoryStream();
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    return ms.ToArray();
}

public static Image ByteArrayToImage(byte[] byteArrayIn)
{
    var ms = new MemoryStream(byteArrayIn);
    Image returnImage = Image.FromStream(ms);
    return returnImage;
}

DBに保存:

OpenFileDialog open = new OpenFileDialog();
if (open.ShowDialog() == DialogResult.OK)
{
    // display image in picture box
    Image img = new Bitmap(open.FileName);
    picBox.Image = img;
    // Byte Array that can store as BLOB in DB
    byte[] blobData = ImageToByteArray(img);
}

DBからロード:

picBox.Image = ByteArrayToImage(/* Byte Array of image from BLOB cell in DB */);
于 2012-07-14T23:35:32.277 に答える