我需要一些代码来帮助我打印存储在数据网格行或SQL DB图像上的图像,我想用它做一些有用的事情。
我的观点是每次点击数据流时插入图像。
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Trabalhos\AuthMyRegistery\AuthMyRegistery\Data.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
id_Worker_Info = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["id_Worker_Info"].Value.ToString());
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from Worker where id_Worker_Info='" +id_Worker_Info+ "';";
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
dataGridView1.DataSource = dt;
foreach (DataRow dr in dt.Rows)
{
WorkerIdTextBox.Text = dr["id_Worker_Info"].ToString();
NameTextBox.Text = dr["Name"].ToString();
FnameTextBox.Text = dr["Formation_Name"].ToString();
BirthTextBox.Text = dr["Date_of_Birth"].ToString();
}
}
答案 0 :(得分:0)
假设您的数据来自字节数组,例如包含有效图像数据的blob类型:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
struct contact {
char name[29];
char number[11];
};
int main() {
struct contact contact[100];
int menu;
int n = 0;
int i, j, flag_1, flag_2, flag_3, flag_4, loop;
char temp[29];
do {
loop = 0;
printf("Phone Book\n");
printf("==========\n");
printf("1. Add New Contact\n");
printf("2. View List Contact\n");
printf("3. Search Contact\n");
printf("4. Delete Contact\n");
printf("5. Exit\n");
printf("choose menu: ");
scanf("%d", &menu);
system("cls");
switch (menu) {
case 1:
printf("Add New Contact\n");
do {
flag_1 = flag_2 = 1;
printf("Input name [1..30 char]: ");
scanf(" %[^\n]", contact[n].name);
if (strlen(contact[n].name) > 30) {
printf("Length of name should be between 1 and 30 characters.\n");
flag_1 = 0;
}
if (!isalpha(contact[n].name[0])) {
printf("First letter of name should be an alphabet (A-Z od a-z).\n");
flag_1 = 0;
}
} while (flag_1 == 0 || flag_2 == 0);
do {
flag_3 = flag_4 = 1;
printf("Input phone number [6..12 digits]: ");
scanf("%s", contact[n].number);
if (strlen(contact[n].number) < 6 || strlen(contact[n].number) > 12) {
printf("Length of phone numbers should be between 6 and 12 digits.\n");
flag_3 = 0;
}
for (i = 0; i < strlen(contact[n].number); i++) {
if (!isdigit(contact[n].number[i])) {
flag_4 = 0;
}
}
if (flag_4 == 0) {
printf("Phone numbers should only contain digits (0-9).\n");
}
} while (flag_3 == 0 || flag_4 == 0);
printf("\n");
printf("New contact successfully added!\n\n");
printf("Name : %s\n", contact[n].name);
printf("Number : %s\n\n", contact[n].number);
printf("Press Enter to continue...");
n++;
getchar();
getchar();
system("cls");
loop = 0;
break;
case 2:
printf("View List Contact\n\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n - 1; j++) {
if (strcmp(contact[j].name, contact[j + 1].name) > 0) {
strcpy(temp, contact[j].name);
strcpy(contact[j].name, contact[j + 1].name);
strcpy(contact[j + 1].name, temp);
strcpy(temp, contact[j].number);
strcpy(contact[j].number, contact[j + 1].number);
strcpy(contact[j + 1].number, temp);
}
}
}
printf(" # Name Phone Number\n");
for (i = 0; i < n; i++) {
printf(" %d %s %s\n", i + 1, contact[i].name, contact[i].number);
}
getchar();
getchar();
system("cls");
loop = 0;
break;
}
} while (loop == 0);
getchar();
return 0;
}
由您自行决定 using System.IO;
..
byte[] iData = (byte[])dr[yourColumnIndexOrName];
Bitmap bmp = null;
if (iData != null)
using (var ms = new MemoryStream(iData ))
{
bmp = new Bitmap(ms);
}
pb_image.Image = bmp; // display in a PictureBox
和SizeMode
的{{1}}。如果您愿意,可以测试Size
。
在分配新位图之前,不要忘记清理位图:
PictureBox