binary to image c# nasıl yapılır
Merhaba,
binary to image c# nasıl yapılır kodları takip ederek nasıl yapıldığını anlayabilirsiniz.Ekte örnek proje var
Kodları aşağıda paylaşıyorum
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
namespace binary_to_image
public partial class Form1 : Form
string murat = “”;
public Form1()
InitializeComponent();
private void ResimFormat(object sender, ConvertEventArgs e)
// e.Value DB den gelen orjinal value
try
Byte[] img = (Byte[])e.Value;
System.IO.MemoryStream ms = new System.IO.MemoryStream();
ms.Write(img, 0, img.Length);
Bitmap bmp = new Bitmap(ms);
ms.Close();
e.Value = bmp;
catch
/* Eğer database e resim kaydedilmemiş ise boş bir resim oluşturup value olarak gönderiyoruz. Eğer yapmazsak program hata verecektir
*/
Bitmap b = new Bitmap(10, 10);
e.Value = b;
private byte[] ResimCevir(string path)
byte[] Resim;
try
System.IO.FileStream Fs =
new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader Br = new System.IO.BinaryReader(Fs);
Resim = Br.ReadBytes((int)Fs.Length);
Br.Close();
Fs.Close();
catch
Resim = null;
return Resim;
private void button1_Click(object sender, EventArgs e)
OpenFileDialog opd = new OpenFileDialog();
if (opd.ShowDialog() == DialogResult.OK && opd.FileName.Length > 3)
pictureBox1.Image = Image.FromFile(opd.FileName);
SqlConnection sqlConn = new SqlConnection();
sqlConn.ConnectionString = “server=GOKDENIZX\\SQLEXPRESS;user=sa;pwd=;database=Resimler;”;
//connection string ile hangi bilgisayarın hangi data baseine hangi kullanıcı adı we şifryle baglanılacagını belirler
sqlConn.Open();//baglantı kurulur
SqlCommand komut = new SqlCommand(“insert into resimtablosu(PersAdSoyad,resim) values(‘murat’,@p1)”,sqlConn);
komut.Parameters.Add(“@p1”, SqlDbType.Image).Value = ResimCevir(opd.FileName);
komut.ExecuteNonQuery();
sqlConn.Dispose();
sqlConn.Close();
private void button2_Click(object sender, EventArgs e)
SqlConnection sqlConn = new SqlConnection();
sqlConn.ConnectionString = “server=GOKDENIZX\\SQLEXPRESS;user=sa;pwd=;database=Resimler;”;
//connection string ile hangi bilgisayarın hangi data baseine hangi kullanıcı adı we şifryle baglanılacagını belirler
sqlConn.Open();//baglantı kurulur
SqlDataAdapter Da = new SqlDataAdapter(“Select resim from resimtablosu”, sqlConn);
DataTable Dt = new DataTable();
Da.Fill(Dt);
Binding bdResim = new Binding(“Image”, Dt, “Resim”);
bdResim.Format += new ConvertEventHandler(ResimFormat);
pictureBox1.DataBindings.Add(bdResim);
sqlConn.Dispose();
sqlConn.Close();
Örnek Proje : Burdan indirin
binary to image c# nasıl yapılır
Hiç yorum yok:
Yorum Gönder