如何将图像转换为字节 [英] how to convert image to byte

查看:79
本文介绍了如何将图像转换为字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

这是我的代码,我在将图像转换为字节格式时遇到问题.
值也在数据库中填充,但是当我想在gridview中显示图像时,它什么也没显示.

请帮忙....
我不明白这是问题还是其他问题
这里从数据库中填充了空值....

Hi everyone,

Here is my code, I am facing problem in converting image to byte format.
Also value is getting populated in database but when I want to show the image in gridview it shows nothing.

Please help....
I dont understand if this is the problem or anything else is the problem
here null value is getting populated from database....

 public byte[] ShowEmpImage(int wids)
{
    int flag = 0;

    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["WATERMARKING"].ConnectionString);
    connection.Open();
    SqlCommand imid = new SqlCommand("SELECT IMAGE_ID from WATERMARKED_IMAGES", connection);
    SqlCommand wid = new SqlCommand("SELECT WID from WATERMARKED_IMAGES where FLAG=''" + flag + "''", connection);
    wids = Convert.ToInt32(wid.ExecuteScalar());
    SqlCommand photoCommand = new SqlCommand("SELECT IMAGE.IMAGE_CONTENT  FROM   WATERMARKED_IMAGES INNER JOIN   IMAGE ON WATERMARKED_IMAGES.IMAGE_ID = IMAGE.IMAGE_ID where WATERMARKED_IMAGES.FLAG=''" + flag + "''  ", connection);
       // Get bytes return from DB Command
    byte[] b = (byte[])photoCommand.ExecuteScalar();
    if (b.Length > 0)
    {
        return b;
    }



}

推荐答案

您发布了太多代码.我没有费心去看它.使用google,我在CP文章中找到了以下代码:

You posted way too much code. I didn''t bother to look through it. Using google, I found this code in a CP article:

public byte[] ImageToByteArray( System.Drawing.Image p_ImageIn )
{
    byte[] aRet = null;
    using ( System.IO.MemoryStream oMS = new System.IO.MemoryStream() )
    {
        p_ImageIn.Save( oMS, System.Drawing.Imaging.ImageFormat.Gif );
        aRet = oMS.ToArray();
    }
    // Possibly dispose image too here, if no extra manipulation is needed
    // Remember, Image is sent by ref, so if you dispose it inside method it will 
    // not be available outside either.
    // p_ImageIn.Dispose();
    return aRet;
}


您应该研究这些

这篇关于如何将图像转换为字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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