0

如果我的pictureboxstupic 包含图像,我在我的一个项目中有一个方法如下返回字节[]。虽然我想以这样一种方式转换它,如果pictureboxstupic中没有图片,它应该返回string =“NULL”,否则它应该返回bute []。我怎样才能做到这一点??

private byte[] GetPic()
{
    using (var stream = new MemoryStream())
    {
        var bmp = new Bitmap(pictureboxstupic.Image);
        bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
        stream.Position = 0;
        var data = new byte[stream.Length];
        stream.Read(data, 0, data.Length);
        return data;
    }
}

注意:我插入图像的列如下

CREATE TABLE [dbo].[Students] 
([Photo] [image] NULL)

在我目前的情况下,我将图像插入到我上面提到的列中,即 Student.Photo 像这样

if (pictureboxstupic.Image == null)
{
    cmd.Parameters.Add("@Photo", SqlDbType.VarChar).Value = "NULL";
}
else
{
cmd.Parameters.Add("@Photo", SqlDbType.Image).Value = GetPic();
}
4

6 回答 6

4

您可能总是返回对象并将其转换为您期望的类型 - 但这不是一个好主意。最好在调用方法之前检查pictureboxstupic 是否为空,然后执行正确的逻辑(例如分配“NULL”)。

您也可以对TryParse应用类似的语义- 也就是说,如果 pictureboxstupic 不为空,您可能会返回 bool 并返回字节数组作为输出参数。

于 2012-05-06T08:03:25.887 回答
3

@empi 的答案是正确的,我想对其进行扩展:方法和函数应该有一组严格定义的职责。就您的GetPic方法而言,它是获取图像的字节流。这应该是该方法中封装的逻辑的范围。

在某些错误框中显示单词“NULL”不是 GetPic我们的责任。调用它的人负责显示图像或错误消息。这就是为什么我认为如果不接收图像是一个合理的条件就GetPic应该返回null,或者如果它是一个破坏程序流程的真正错误条件,甚至可能抛出异常。

在任何情况下你都不应该做的是返回一个object可以是byte[]orstring的,因为这违背了强类型环境的全部要点,并且是稍后引入错误的好方法。如果您必须返回 abyte[]或 a string,您可以使用ref/out其他人建议的参数,或者创建一个名为 的新类ImageData,或者其他包含字节和错误消息的类,您可以使用它来检索一个或其他。

- 编辑

在你澄清之后,我仍然会坚持我的回答。你不应该那样做。数据库支持与字符串“NULL”不同的NULL 值。而不是插入此字符串,您应该确保您的列可以为空,并插入空值 - DBNull.Value。您当前的代码将崩溃,因为 Photo 列无法处理 VarChar 值。

于 2012-05-06T08:19:22.093 回答
1

object而是返回?然后通过首先使用is关键字检查类型来进行强制转换。

if(returnedType is string) //or use as and then check for NULL
{

}
else   //type is byte[]
{
}

不确定您的用例以及它是否有效,但这可能是一个解决方案

于 2012-05-06T08:03:44.160 回答
1

您可以通过三种解决方案来实现:

1.) 使用对象作为返回类型

2.) 使用 'ref' 和 'out' 参数说明符返回数组并使方法返回类型字符串用于返回状态消息。

3.) 对指针使用“不安全”代码...

例子:

    public string GetPic(out byte[] ret)
    {
        if (Image == null)
            return "NULL";
        //set ret's value as image bytes
        return "ok";
    }
于 2012-05-06T08:08:01.540 回答
0

你可以这样做:

        //call the method:
        string str = ""; //variable that will be changed accordingly by returned value
        byte[] image = GetPic();
        if (image.Length == 0)
        {
            //do what ever you want, like string = "NULL":
            str = "NULL";
        }
    //
    //

    private byte[] GetPic()
    {
        PictureBox pictureboxstupic = new PictureBox();
        using (var stream = new MemoryStream())
        {
            var bmp = new Bitmap(pictureboxstupic.Image);
            bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            stream.Position = 0;
            var data = new byte[stream.Length];
            stream.Read(data, 0, data.Length);
            return data;
        }
    }
于 2012-05-06T08:26:07.163 回答
-1

我用这个方法解决了我的问题:

private object GetPic()
{
    if(pictureboxstupic.Image == null)
    {
        return null;
    }
    using (var stream = new MemoryStream())
    {
        var bmp = new Bitmap(pictureboxstupic.Image);
        bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
        stream.Position = 0;
        var data = new byte[stream.Length];
        stream.Read(data, 0, data.Length);
        return data;

    }
}

用法:

cmd.Parameters.Add("@Photo", SqlDbType.Image).Value = GetPic() ?? DBNull.Value;
于 2012-05-06T11:48:26.173 回答