将二进制数据转换为图像 [英] to convert binary data to image

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

问题描述

亲爱的朋友,

我已经通过文件上传传递了一个图像,并将其转换为二进制数据,并将其存储在数据库中.它成功完成了,但是当我尝试从数据库中返回它并显示在图像控件中时,它显示错误
这是我的代码

dear friends,

i have passed an image athrough fileupload and convert it to binary data and store it in database. it sucesfuly completed but when i am trying to return it from database and shown in image control it show error
here is my code

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;

public partial class Default11 : System.Web.UI.Page
{
    SqlConnection     cc  = new SqlConnection("server=nilu-pc;initial catalog=project;integrated security=true");
    DataSet           ds  = new DataSet();
    public SqlCommand cmd = new SqlCommand();
    SqlDataAdapter adp;

    protected void Page_Load(object sender, EventArgs e)
    {
        cc.Open();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        BinaryReader rr=new BinaryReader(FileUpload1.PostedFile.InputStream);

        byte[] img = rr.ReadBytes(FileUpload1.PostedFile.ContentLength);
       
        string sql = "insert into image values(1,@IMG)";
        adp = new SqlDataAdapter(sql, cc);
        adp.SelectCommand.Parameters.Add("@IMG",SqlDbType.VarBinary,8000).Value=img;
        adp.Fill(ds,"image");
    }

    protected void showinimagecontrol_Click(object sender, EventArgs e)
    {
        System.Drawing.Image imaf = null;
        SqlCommand command = cc.CreateCommand();
        command.CommandText = "select IMG from image WHERE ID=1";
     
        //adp = new SqlDataAdapter(sql,cc);
        //adp.Fill(ds, "image 
        byte[] imageData = (byte[])command.ExecuteScalar();

        // byte[] imgdata = (byte[])ds.Tables[0].Rows[0][1];
        Response.Write(imageData);
        MemoryStream mm = new MemoryStream(imageData, 0, imageData.Length);
        imaf = System.Drawing.Image.FromStream(mm);
        imaf.Save(Server.MapPath("Photo0074.jpg"));
        Image1.ImageUrl = "Photo0074.jpg";
    }
}



它在image.fromstream行中将错误显示为无效参数.

帮助我纠正错误

谢谢



it show error as invalid parameter in image.fromstream line

help me in rectifying error

thanks

推荐答案

该流为null,或者它不是有效的图像格式.

使用调试器找出您遇到的问题.
Either the stream is null, or it isn''t a valid image format.

Use the debugger to find out which problem you''re having.


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

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