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

我正在尝试检索存储在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;


  

MYYA
浏览 183回答 2
2回答

慕哥9229398

经过更多的努力,我发现无法将JPEG图像保存到“内存流”中。这产生了普遍存在的通用GDI错误。必须先使用以下代码将JPEG文件转换为bitmp图像,然后才能显示它们:(var ms = new MemoryStream()){&nbsp; &nbsp; Bitmap bmp = new Bitmap(imageToConvert);&nbsp; &nbsp; bmp.Save(ms, format);&nbsp; &nbsp; return ms.ToArray();}另一个注意事项:JPEG文件从SQL Filestream作为位图的图像类型返回,这就是我尝试与@Amir Atasin的条目一起尝试的原因:GDI +中发生了一般性错误,JPEG图像到MemoryStream
打开App,查看更多内容
随时随地看视频慕课网APP