本文将为您提供关于从数据库中提取多个图像到PictureBox中的详细介绍,我们还将为您解释从数据库获取图片的相关知识,同时,我们还将为您提供关于c#pictureBox循环播放图片、C#pictur
本文将为您提供关于从数据库中提取多个图像到PictureBox中的详细介绍,我们还将为您解释从数据库获取图片的相关知识,同时,我们还将为您提供关于c# pictureBox 循环播放图片、C# pictureBox用法案例详解、c# – 从数据库中加载PictureBox图像、c# – 删除在picturebox中显示的文件的实用信息。
本文目录一览:- 从数据库中提取多个图像到PictureBox中(从数据库获取图片)
- c# pictureBox 循环播放图片
- C# pictureBox用法案例详解
- c# – 从数据库中加载PictureBox图像
- c# – 删除在picturebox中显示的文件
从数据库中提取多个图像到PictureBox中(从数据库获取图片)
我想从我的数据库中获取多个图像,其中data是图像的字段。 这是我的代码,但它只显示第一张图片。 请帮忙。
sqlConnection sq = new sqlConnection(@"Data Source=DESKTOP-GH3KCDHsqlEXPRESS;Initial Catalog=Project1;User ID=sa;Password=Salma0300."); String st = "select data FROM Picture"; sq.open(); PictureBox[] pb = { pictureBox1,pictureBox2,pictureBox3,pictureBox4,pictureBox5,pictureBox6,pictureBox7,pictureBox8}; MemoryStream stream = new MemoryStream(); sqlCommand sqlcom = new sqlCommand(st,sq); byte[] image = (byte[])sqlcom.ExecuteScalar(); stream.Write(image,image.Length); Bitmap bitmap = new Bitmap(stream); pb[1].Image = bitmap; pb[2].Image = bitmap;
在Crystal Reports中导致“页眉和页面页脚太大”页面错误的原因是什么?
通过.NET / C#发送传真
文件被另一个进程使用。 如何知道哪个进程?
如何以编程方式检索“Program Files”文件夹的实际path?
获取当前login的远程机器上的交互式用户?
你有一些问题。
即使您选择了整个表格,也只能抓取1张图片。
byte[] image = (byte[])sqlcom.ExecuteScalar();
你需要遍历行。 使用ExecuteReader()而不是ExecuteScalar为了得到一个sqlDataReader对象。 然后继续从阅读器调用Read() ,直到你没有更多的记录或没有更多的图片框来填充。
https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader(v=vs.110).aspx
您只使用一个位图
Bitmap bitmap = new Bitmap(stream); pb[1].Image = bitmap; pb[2].Image = bitmap;
看到问题? 你加载了一个单一的bitmap并将您的框设置为完全相同的图像。 迭代上面提到的所有记录本质上应该允许你看到这个问题,并纠正这个问题。
c# pictureBox 循环播放图片
c#
1.遍历目录 查找图片 2.在 pictureBox 循环播放
public void PlayThread()//CMD_UpdateBtnStatus cmd
{
Int32 framerate = 30;
Int32 interval = 1000 / framerate;
string suffix = "*.jpg";
List<String> list = Utility.TraverseDirector(videoFramePath, false, true, suffix);
if (list.Count == 0)
{
return;
}
UInt32 count = (UInt32)list.Count;
while (_ImagePlayThreadLoop)
{
for (UInt32 index = 0; index < count; index++)
{
//this.Invoke((EventHandler)(delegate //数据返回错误
//{
//Tab4_UVC_pictureBox.Load(list[(Int32)index]); //同步方式 必须要delegate 方式更新(手动添加 异步方式)
//不能在这里sleep 会卡死UI线程
//}));
//直接用异步方式,更新图片数据
//文件不存在就continue;
if (!File.Exists(list[(Int32)index])) continue;
Tab4_UVC_pictureBox.LoadAsync(list[(Int32)index]);
if (!_ImagePlayThreadLoop)
{
index = count;
//continue;
return;
}
if (index >= count)
{
index = 0;
}
Thread.Sleep(interval);
}
}
}
C# pictureBox用法案例详解
PictureBox 控件可以显示来自位图、图标或者元文件,以及来自增强的元文件、JPEG 或 GIF 文件的图形。如果控件不足以显示整幅图象,则裁剪图象以适应控件的大小。
本文利用openfiledialog控件实现图片文件的打开:
展示了图片控件的sizeMode四种格式:最好的应该是zoom,在图片不发生形变的条件下,对图片进行缩放。
sizemode:autosize–让picturebox适应图片尺寸,zoom–让图片适应picturebox
控件的SizeMode属性,有四种情况:
Normal:图片大小不变;
strechImage:拉伸图片适应PictureBox(图片会变形)
AutoSize:PictureBox适应图片;
CenterImage:图片居中显示;
Zoom:图片填充PictureBox(不变形)
首先向窗体拖动一个openfiledialog控件:
然后在按钮控件中添加代码:
private void button3_Click(object sender, EventArgs e) { //指定查找的文件类型 openFileDialog1.Filter = "@.Jpg|*.jpg|@.Png|*.png|@.Gif|*.gif|@.All files|*.*"; //该对话框会返回一个DialogResult类型的值,DialogResult.OK或者DialogResult.NO DialogResult dr = openFileDialog1.ShowDialog(); if (dr == DialogResult.OK) { //通过输入文件目录,文件模式,访问模式等参数,通过流打开文件 FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read); //通过调用系统的画笔工具,画出一个Image类型的数据,传给pictureBox。 Image im = System.Drawing.Bitmap.FromStream(fs); pictureBox1.Image = im; pictureBox2.Image = im; pictureBox3.Image = im; pictureBox4.Image = im; } }
到此这篇关于C# pictureBox用法案例详解的文章就介绍到这了,更多相关C# pictureBox用法内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
- C# 制作PictureBox圆形头像框并从数据库中读取头像
- C# PictureBox图片控件实现图片交换
- C# 使用PictureBox实现图片按钮控件的示例步骤
- C#控件picturebox实现画图功能
- C# PictureBox控件方法参数及图片删除重命名上传详解
c# – 从数据库中加载PictureBox图像
任何帮助将不胜感激.
private void button1_Click(object sender,EventArgs e) { sql = new sqlConnection(@"Data Source=PC-PC\PC;Initial Catalog=Test;Integrated Security=True"); cmd = new sqlCommand(); cmd.Connection = sql; cmd.CommandText = ("select Image from Entry where EntryID =@EntryID"); cmd.Parameters.AddWithValue("@EntryID",Convert.ToInt32(textBox1.Text)); }
解决方法
// your code first here.. var da = new sqlDataAdapter(cmd); var ds = new DataSet(); da.Fill(ds,"Images"); int count = ds.Tables["Images"].Rows.Count; if (count > 0) { var data = (Byte[])(ds.Tables["Images"].Rows[count - 1]["Image"]); var stream = new MemoryStream(data); pictureBox1.Image= Image.FromStream(sream); }
c# – 删除在picturebox中显示的文件
我搜索了很多这个异常得到解决,但我没有任何工作,当我试图关闭文件中的imagename的文件,即我在图片框中显示的文件;使用IsFileLocked方法,这会关闭和删除特定目录路径的所有文件,但是如何删除在pictureBox中显示的唯一文件,我将出错
public partial class RemoveAds : Form { OpenFileDialog ofd = null; string path = @"C:\Users\Monika\Documents\Visual Studio 2010\Projects\OnlineExam\OnlineExam\Image\"; // this is the path that you are checking. public RemoveAds() { InitializeComponent(); } private void button1_Click(object sender,EventArgs e) { if (System.IO.Directory.Exists(path)) { ofd = new OpenFileDialog(); ofd.InitialDirectory = path; DialogResult dr = new DialogResult(); if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { Image img = new Bitmap(ofd.FileName); string imgName = ofd.SafeFileName; txtimageName.Text = imgName; pictureBox1.Image = img.GetThumbnailImage(350,350,null,new IntPtr()); ofd.RestoreDirectory = true; } } else { return; } } private void button2_Click(object sender,EventArgs e) { //Image img = new Bitmap(ofd.FileName); string imgName = ofd.SafeFileName; if (Directory.Exists(path)) { var directory = new DirectoryInfo(path); foreach (FileInfo file in directory.GetFiles()) { if(!IsFileLocked(file)) file.Delete(); } } } public static Boolean IsFileLocked(FileInfo path) { FileStream stream = null; try { //Don't change FileAccess to ReadWrite,//because if a file is in readOnly,it fails. stream = path.Open ( FileMode.Open,FileAccess.Read,FileShare.None ); } catch (IOException) { //the file is unavailable because it is: //still being written to or being processed by another thread //or does not exist (has already been processed) return true; } finally { if (stream != null) stream.Close(); } //file is not locked return false; } }
提前感谢任何帮助
解决方法
The file remains locked until the Bitmap is disposed.
在您的代码中,您创建位图并将其存储在本地变量中,但在完成后您永远不会处理它.这意味着您的图像对象已超出范围,但尚未释放其对要删除的图像文件的锁定.对于实现Idisposable的所有对象(如Bitmap),您必须自己处理它们.例如参见this question(或搜索其他人 – 这是一个非常重要的概念!).
要正确解决问题,您只需要处理完成后的图像:
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { Image img = new Bitmap(ofd.FileName); // create the bitmap string imgName = ofd.SafeFileName; txtimageName.Text = imgName; pictureBox1.Image = img.GetThumbnailImage(350,new IntPtr()); ofd.RestoreDirectory = true; img.dispose(); // dispose the bitmap object }
请不要在下面的答案中采取建议 – 你几乎不需要调用GC.Collect,如果你需要做它来使事情工作,它应该是一个非常强大的信号,你正在做其他错误.
此外,如果您只想删除一个文件(您显示的位图),您的删除代码是错误的,并将删除目录中的每个文件(这只是重复Adel的要点).此外,与其保留一个全局的OpenFileDialog对象只是存储文件名,我建议摆脱那个,只保存文件信息:
FileInfo imageFileinfo; //add this //OpenFileDialog ofd = null; Get rid of this private void button1_Click(object sender,EventArgs e) { if (System.IO.Directory.Exists(path)) { OpenFileDialog ofd = new OpenFileDialog(); //make ofd local ofd.InitialDirectory = path; DialogResult dr = new DialogResult(); if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { Image img = new Bitmap(ofd.FileName); imageFileinfo = new FileInfo(ofd.FileName); // save the file name string imgName = ofd.SafeFileName; txtimageName.Text = imgName; pictureBox1.Image = img.GetThumbnailImage(350,new IntPtr()); ofd.RestoreDirectory = true; img.dispose(); } ofd.dispose(); //don't forget to dispose it! } else { return; } }
然后在您的第二个按钮处理程序中,您可以删除您感兴趣的一个文件.
private void button2_Click(object sender,EventArgs e) { if (!IsFileLocked(imageFileinfo)) { imageFileinfo.Delete(); } }
今天关于从数据库中提取多个图像到PictureBox中和从数据库获取图片的介绍到此结束,谢谢您的阅读,有关c# pictureBox 循环播放图片、C# pictureBox用法案例详解、c# – 从数据库中加载PictureBox图像、c# – 删除在picturebox中显示的文件等更多相关知识的信息可以在本站进行查询。
本文标签: