簡體   English   中英

如何顯示以HTML格式從數據庫返回的SQL Server filestream .jpg圖像

[英]How do I present a SQL Server filestream .jpg image returned from the database in HTML

我正在嘗試檢索存儲在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;

        using (TransactionScope ts = new TransactionScope())
        {
            using (SqlConnection conn = new SqlConnection(ConnStr))
            {
                conn.Open();

                using (SqlCommand cmd = new SqlCommand(SelectTSql, conn))
                {
                    cmd.Parameters.Add("@PhotoId", SqlDbType.Int).Value = photoId;

                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        rdr.Read();
                        desc = rdr.GetSqlString(0).Value;
                        serverPath = rdr.GetSqlString(1).Value;
                        serverTxn = rdr.GetSqlBinary(2).Value;
                        rdr.Close();
                    }
                }
                photo = LoadPhotoImage(serverPath, serverTxn);
            }

            ts.Complete();
        }

        return photo;
    }

    private static Image LoadPhotoImage(string filePath, byte[] txnToken)
    {
        Image photo;

        using (SqlFileStream sfs =
          new SqlFileStream(filePath, txnToken, FileAccess.Read))
        {
            photo = Image.FromStream(sfs);
            sfs.Close();
        }

        return photo;
      }

  }
}

嘗試尋找答案3天后,以下方法起作用。 總結,一旦iPhotoImage從數據庫SQLFilestream代碼返回,然后使用TurnImageToByteArray將其轉換為字節數組,然后將imgBytes字節轉換為字符串imgString,以HTML形式顯示圖像。 以下是其他代碼:

@{
//Get the file ID you want to display
int iFileID = 8;
string sPhotoDesc = "";
Image iPhotoImage = null;

iPhotoImage = PhotoLibraryApp.PhotoData.SelectPhoto(iFileID, out 
sPhotoDesc);

byte[] imgBytes = 
PhotoLibraryApp.PhotoData.TurnImageToByteArray(iPhotoImage);
string imgString = Convert.ToBase64String(imgBytes);

}

我在PhotoData類中添加了以下方法:

  public static byte[] TurnImageToByteArray(Image img)
    {
        MemoryStream ms = new MemoryStream();
        img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        return ms.ToArray();
    }

以下是顯示圖像的html:

 <img id="JpegImage" alt="Photo Alternative" src="data:image/jpeg;base64, @imgString">

經過更多的努力,我發現無法將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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM