为了账号安全,请及时绑定邮箱和手机立即绑定

如何以HTML格式显示从数据库返回的SQL Server filestream .jpg图像

如何以HTML格式显示从数据库返回的SQL Server filestream .jpg图像

C#
MYYA 2021-04-30 14:17:59
我正在尝试检索存储在SQL Server中的jpeg文件,并使用img src =“ filepath”将这些文件呈现为html,但是我的C#代码返回的是实际图像,而不是图像的URL路径,这只会显示一个带有x在里面。如何直接将图像显示为图像变量。这是带有Razor C#的HTML页面:@{ int iFileID = 8;string sPhotoDesc = "";Image iPhotoImage = null;}<form><fieldset>    <legend>FileInput</legend>    <input type="file" id="fileinput" />    <input type='button' id='btnLoad' value='Load' onclick="@{iPhotoImage =          PhotoLibraryApp.PhotoData.SelectPhoto(iFileID, out sPhotoDesc); }">    <div id="editor"></div></fieldset><img src="@iPhotoImage" width="500" height="377">以下是名为PhotoData.cs的单独文件中的相关C#代码:using System;using System.Data;using System.Data.SqlClient;using System.Data.SqlTypes;using System.Drawing;using System.IO;using System.Transactions;namespace PhotoLibraryApp{    public class PhotoData    {        private const string ConnStr =          "Data Source=.;Integrated Security=True;Initial                                                                  Catalog=PhotoLibrary;";     public static Image SelectPhoto(int photoId, out string desc)      {        const string SelectTSql = @"        SELECT        Description,        Photo.PathName(),        GET_FILESTREAM_TRANSACTION_CONTEXT()      FROM PhotoAlbum      WHERE PhotoId = @PhotoId";        Image photo;        string serverPath;        byte[] serverTxn;  
查看完整描述

2 回答

?
慕哥9229398

TA贡献1877条经验 获得超6个赞

经过更多的努力,我发现无法将JPEG图像保存到“内存流”中。这产生了普遍存在的通用GDI错误。必须先使用以下代码将JPEG文件转换为bitmp图像,然后才能显示它们:


(var ms = new MemoryStream())

{

    Bitmap bmp = new Bitmap(imageToConvert);

    bmp.Save(ms, format);

    return ms.ToArray();

}

另一个注意事项:JPEG文件从SQL Filestream作为位图的图像类型返回,这就是我尝试与@Amir Atasin的条目一起尝试的原因:GDI +中发生了一般性错误,JPEG图像到MemoryStream


查看完整回答
反对 回复 2021-05-15
  • 2 回答
  • 0 关注
  • 181 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信