显示来自byte []的图像,每次转换为Image时都会显示“无效参数”

时间:2019-03-22 18:41:39

标签: c# asp.net-mvc razor devextreme

我一直试图显示数据库中的图像,当我尝试将byte []转换为图像时,出现“无效参数” 我正在使用一个dll连接到我的数据库,这实际上是为了加快连接速度,最后我得到一个包含查询信息的数据表

我尝试使用MemoryStream,使用IFormatter并对数据表进行序列化,这样我就可以获得byte [],最近我尝试将byte []转换为base64string,当我尝试返回“图片”(但这是因为我使用了公共图片loadPhoto())

这是我要上传的代码

public Image uploadPhoto()
    {
        string strServidor = "srv1";            
        speedDLL _conex = new speedDLL(strServidor, "SCHOOL");
        var file = Request.Files["file"];//This is from a Devextreme component
        int maxFileSize = 1024 * 1536;
        string fileExtension = Path.GetExtension(file.FileName);

        if (fileExtension.ToLower() == ".jpg" || fileExtension.ToLower() == ".png")
        {
            if (file.ContentLength < maxFileSize)
            {
                MemoryStream target = new MemoryStream();
                file.InputStream.CopyTo(target);
                Image imageIn = Image.FromStream(target);
                byte[] imagen; //= target.ToArray();
                using (MemoryStream ms = new MemoryStream())
                {
                    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    imagen = ms.ToArray();
                }

                    try
                    {
                        _conex.BeginTran();
                        _conex.AgregarParametro("@Imagen", imagen);//This is a method from the dll to put a parameter into the storedprocedure 
                        _conex.AgregarParametro("@IdFlete", "PRUEBAjpg1");//This is the same
                        _conex.EjecutaComando("EXEC PA_ActSubirImag @IdFlete, @Imagen");
                        _conex.CommitTran();
                    }
                    catch (Exception Ex)
                    {
                        _conex.RollBackTran();
                    }

在SQL中,我得到类似0xFFD8FFE000的信息。 这就是我一直尝试检索该图像的方式:

            public Image loadPhoto()
    {
        try
        {
            string strServidor = "srv1";            
        speedDLL _conex = new speedDLL(strServidor, "SCHOOL");

            object imagenSQL = _conex.EjecutaConsulta("EXEC PA_ConTraerImag @IdFlete = PRUEBAjpg1");
            MemoryStream stream = new MemoryStream();
            System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            formatter.Serialize(stream, imagenSQL);
            byte[] imagen = stream.GetBuffer();
            //MemoryStream imagenStream = new MemoryStream(stream.GetBuffer());

            //base64StringIm = base64StringIm.Replace("data:image/jpeg;base64,", "");
            //Image convertToImage = Image.FromStream(new MemoryStream(imagen));
            Image imagenReturn = null;//Parametro no valido
            using (MemoryStream ms = new MemoryStream(imagen))
            {
                imagenReturn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                //imagenReturn.ImageUrl = "";
                //var base64Im = Convert.ToBase64String(imagen);
                //var imgSrc = String.Format("data:image/jpeg;base64",base64Im);

                return imagenReturn;
            }
// using (MemoryStream ms = new MemoryStream(imagen))
using (Image image = Image.FromStream(ms, true, true))
{
    return (Image)image.Clone();
}
        }

我在评论中做了一些尝试,我使用Image image = new Image.FromStream(),并且得到了“无效参数”相同的结果,我不知道是否有人可以帮助我,也许在制作流时出错,或者也许是我的dll(最后我得到了包含信息的数据表)。 预先感谢。

0 个答案:

没有答案