PDA

View Full Version : سوال: اضافه کردن عکس در بانک و نمایش در دیتا گرید



vahidth
چهارشنبه 15 آذر 1391, 11:26 صبح
سلام به دوستان من کلی جستجو کردم ولی اونچیزی که من میخواستم پیدا نشد
من در این پروژه یک بانک با اطلاعات زیر ساختم
1- id-----int
2-name---nvarchar
3-family nvarchar
4- pic---------- ( varbinary(max
خوب حالا میخوام اطلاعات ای دی نام و فامیل و عکس در دیتابیس ذخیره بشه و وقتی روی دیتاگرید کلیک کردم نمایش داده بشه؟
96071
کد وارد کردن عکس رو بلدم
string filename;
openFileDialog1.Filter = "Images(*.jpg)|*.jpg|All files(*.*)|*.*)";
openFileDialog1.FilterIndex = 1;
openFileDialog1.Title = "browse";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
filename = openFileDialog1.FileName;
pictureBox1.ImageLocation = filename;
}

samadblaj
چهارشنبه 15 آذر 1391, 11:45 صبح
سلام

تابع تبدیل عکس:

byte[] ReadFile(string sPath)
{
//Initialize byte array with a null value initially.
byte[] data = null;

//Use FileInfo object to get file size.
FileInfo fInfo = new FileInfo(sPath);
long numBytes = fInfo.Length;

//Open FileStream to read file
FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);

//Use BinaryReader to read file stream into byte array.
BinaryReader br = new BinaryReader(fStream);

//When you use BinaryReader, you need to supply number of bytes to read from file.
//In this case we want to read entire file. So supplying total number of bytes.
data = br.ReadBytes((int)numBytes);
return data;
}

با این کد هم طبق روشخودتون ذخیره کنید

private void button2_Click(object sender, EventArgs e)
{
//مسیر عکس
byte[] imageData = ReadFile(textBox1.Text);

bankEntities db = new bankEntities();
Picture_Save ps = new Picture_Save()
{
name = "image",
image = imageData,
note = "tttttttt"
};
db.Picture_Save.AddObject(ps);
db.SaveChanges();
dataGridView1.DataSource = db.Picture_Save;
}

Mahmoud Zaad
چهارشنبه 15 آذر 1391, 17:17 عصر
سلام
txtMatn.Text = dataGridViewX1.CurrentRow.Cells["matn"].Value.ToString();

if (dataGridViewX1.CurrentRow.Cells["ax"].Value != DBNull.Value)
{
MemoryStream ms = new MemoryStream((byte[])(dataGridViewX1.CurrentRow.Cells["ax"].Value));
pictureBox1.Image = Image.FromStream(ms);
}
else
{
pictureBox1.Image = null;
}
خط اول برای سلول متنی هست، بقیه برای عکس. اسم فیلد عکس در جدول دیتابیس فرضی ax هستش. همینطور اسم فیلد متنی در دیتابیس فرضی matn هست.

vahidth
چهارشنبه 15 آذر 1391, 17:28 عصر
ممنون ازت داداش
من کد شما رو به این شکل قرار دادم ولی بازم ارور میده در ضمن من نمیدونم بجای (matn) چی بدم من 4 تا فیلد دارم که فیلد عکس نامش اینه(pic)
private void button2_Click(object sender, EventArgs e)
{
textBox4.Text = dataGridView1.CurrentRow.Cells["matn"].Value.ToString();

if (dataGridView1.CurrentRow.Cells["pic"].Value != DBNull.Value)
{
MemoryStream ms = new MemoryStream((byte[])(dataGridView1.CurrentRow.Cells["pic"].Value));
pictureBox1.Image = Image.FromStream(ms);
}
else
{
pictureBox1.Image = null;
}
}
من فقط کد browse رو نوشتم و آقای samadblaj (http://barnamenevis.org/member.php?235579-samadblaj) گفت باید به تابع تبدیل کنی منم که بلد نیستم
داداش پروژه رو هم برات آپلود کردم یه نگاه بنداز
http://uploadtak.com/images/t6317_Project_image.rar

vahidth
چهارشنبه 15 آذر 1391, 19:02 عصر
ممنون میشم اگه یکی منو کمک کنه

Mahmoud Zaad
چهارشنبه 15 آذر 1391, 19:11 عصر
گفتم که اون یه مثال فرضی بود (هم برای فیلد متنی - خط اول و هم برای عکس - بقیه کدها).
شما چه جوری تکست باکسهاتو پر می کنی، همونجوری پر کن، خط اولی که من نوشتم رو کامنت کن اصلاً ! فقط از خط دو به بعد استفاده کن. کد قسمت عکست هم درسته.

vahidth
چهارشنبه 15 آذر 1391, 19:37 عصر
داداش این ارور رو میده میگه نیم اسپیس تعریف کن
96099
داداش این کل کدهای منه
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Project_image
{
public partial class Form1 : Form
{
SqlConnection cnn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirec tory|\db2.mdf;Integrated Security=True");
public Form1()
{
InitializeComponent();

}


private void button1_Click(object sender, EventArgs e)
{
string s = Environment.CurrentDirectory;
string strbk;
openFileDialog1.Filter = "JPEG (*.JPG) | *.jpg|" + "Bitmap Files (*.bmp) | *.bmp";

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
strbk= openFileDialog1.FileName;
pictureBox1.Image = System.Drawing.Bitmap.FromFile(strbk);
}

Environment.CurrentDirectory = s;




}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{
textBox4.Text = dataGridView1.CurrentRow.Cells["matn"].Value.ToString();

if (dataGridView1.CurrentRow.Cells["pic"].Value != DBNull.Value)
{
MemoryStream ms = new MemoryStream((byte[])(dataGridView1.CurrentRow.Cells["pic"].Value));
pictureBox1.Image = Image.FromStream(ms);
}
else
{
pictureBox1.Image = null;
}
}
}
}

Mahmoud Zaad
چهارشنبه 15 آذر 1391, 19:54 عصر
خب از اول متن ارور رو میگفتی، using System.IO;