图片框不显示图像VB.NET [英] Picture box not displaying image VB.NET

查看:293
本文介绍了图片框不显示图像VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



希望有人可以提供帮助...

我使用VARBINARY列将图像存储在SQL数据库中。

它存储好了我可以检索它但我似乎无法让它显示在PictureBox控件中。

当我设置picturebox图像时它似乎设置好了我实际上可以将图片框图像保存到文件没问题,但它不会显示它。

当然这是显而易见的事情,但对于我的生活,我看不出代码有什么问题: 。



Hi All

Hope someone can help...
I am storing images in an SQL database using a VARBINARY column.
It is stored OK and I can retrieve it but I cannot seem to get it to display in a PictureBox control.
When I set the picturebox image it seems to get set OK and I can actually save the picture box image to a file no problem but it will not display it.
Sure it is something obvious but for the life of me I cannot see what is wrong with the code:.

Dim bImage As Byte() = DirectCast(oCmd.ExecuteScalar(), Byte())
If bImage Is Nothing Then
    Return
End If

Dim ms As New IO.MemoryStream
ms.Write(bImage, 0, bImage.Length)
Dim bitmap As New Bitmap(ms)
pictureBox1.Image = bitmap

Dim sf As New SaveFileDialog
If sf.ShowDialog() = DialogResult.OK Then
    MsgBox(sf.FileName)
    pictureBox1.Image.Save(sf.FileName, Imaging.ImageFormat.Jpeg)
End If





如上所述,SQL返回数据然后转换为bi tmap。

检查对象时,一切看起来都不错,即格式,高度,宽度等...

pictureBox1.Image.Save工作正常,原始图像可以从文件资源管理器中查看就好了。



所以......为什么pictureBox1没有显示我的图像?





谢谢



我的尝试:



仔细检查我的代码,确定没问题,谷歌搜索我能想到的一切。

尝试了所有可能的加载图像的方法; FromStream / FromFile等...



As said, the SQL returns data which is then converted to a bitmap.
Everything looks OK when inspecting the objects, i.e. format, height, width etc...
The pictureBox1.Image.Save works perfectly and the original image can be viewed from file explorer just fine.

So... why does the pictureBox1 not show my image?


Thanks

What I have tried:

Checked my code carefully, sure it is OK, googled everything I can think of.
Tried all possible methods to load the image; FromStream / FromFile etc...

推荐答案

在分配位图后,您正在显示文件选择对话框,该位图阻止绘制图片框控件的新内容。 />


如果位图没有错误,一旦关闭文件选择对话框并返回设置图像的功能,就应显示新图像。



要在打开文件对话框之前显示图像,可以强制重绘:

You are showing a file selection dialog just after assigning the bitmap which blocks drawing the new content of the picture box control.

If there was no error with the bitmap, the new image should be shown once the file selection dialog is closed and the function where you set the image returns.

To show the image before opening the file dialog, you can force a redraw:
pictureBox1.Image = bitmap
pictureBox1.Refresh


好的,这里发生了什么以及如何诊断这些错误,万一有人在搜索时发现这个错误。



要诊断确保您的初始图像,图像和错误图像都不同。

这样您就可以立即看到它是否有错误。



如果出错,请使用picturebox.LoadComplete事件。

查看e.Err或者瞧瞧出现错误图像的原因。
如果控制权在我的拙见中提出异常会更好但它就是这样......



希望这对某人有所帮助。



BTW,所以你们都可以嘲笑我,我失败的原因是由于价值无效在图像位置属性中。我认为这意味着图片框中的位置(我想要渲染图像),所以我将其设置为中心LOL。它没有在设计时抱怨,但在运行时它实际上是在寻找一个名为c:/ projectfolder / center的文件位置来加载图像。 whoopsie。



感谢您的回复。
OK here is what happened and how to diagnose such errors just in case anyone finds this whilst searching.

To diagnose make sure your initial image, Image and Error Image are all different.
That way you can instantly see if it is erroring.

If it is erroring then use the picturebox.LoadComplete event.
Have a look at e.Error and voila the reason for the error image is revealed.
Would be better if the control threw an exception in my humble opinion but it is what it is...

Hope this helps someone.

BTW, so you can all laugh at me, the reason mine was failing was due to an invalid value in the image location property. I thought it meant where (location) in the picture box did I want the image to be rendered so I set it to centre LOL. It did not complain at design time but at runtime it was actually looking for a file location called c:/projectfolder/centre to load the image from. whoopsie.

Thanks for the replies.


这篇关于图片框不显示图像VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆