1

データベースに保存されている画像のデータをbyte[]配列として取得します。次に、以下に示すコードのようにSystem.Drawing.Imageに変換します。

  public System.Drawing.Image CreateImage(byte[] bytes)
        {

            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(bytes);
            System.Drawing.Image image = System.Drawing.Image.FromStream(memoryStream);
            return image;
        }

(*)一方、クライアントがページを下にスクロールすると、asp.netページに画像のリストを表示する予定です。より多くのユーザーがページを上下に移動すると、より多くの写真が表示されます。つまり、ページの読み込みが速く、ユーザーエクスペリエンスが豊富であることを意味します。(www.mashable.comで私が何を意味するかがわかるかもしれませんが、下にスクロールするときに写真の新しいロードに注意してください。)

さらに、上記のメソッドから返されたimgaeオブジェクトは、上記の(*)条件を使用して動的にループで表示するにはどうすればよいですか。

よろしくbk

4

1 に答える 1

1

さて、主なボトルネックは、画像が必要になるたびに実際にデータベースにアクセスすることだと思います。(特に、サイトにアクセスする多くのユーザーを考慮してください。)

私は次の解決策を選びます:

  1. データベースは元の品質の画像を保存します。
  2. .ashxハンドラーは、必要なさまざまな解像度(アイコンの場合は32x32ピクセル、サムネイルの場合は48x48など)でファイルシステムに画像をキャッシュし、要求に応じて画像を返し、データベースに1回だけアクセスします。(この例では、ashxハンドラーを介して画像を返す方法を示しています)
  3. 実際のページは、画像を取得するために.ashxページを指します。(のように<img scr="GetImage.ashx?ID=324453&Size=48" />

アップデート:

したがって、ハンドラーの実際のワークフローは次のようになります。

    public void ProcessRequest (HttpContext context)
    {
        // Create path of cached file based on the context passed
        int size = Int32.Parse(context.Request["Size"]);
        // For ID Guids are possibly better
        // but it can be anything, even parameter you need to pass
        // to the web service in order to get those bytes
        int id = Int32.Parse(context.Request["Id"]);
        string imagePath = String.Format(@"images/cache/{0}/{1}.png", size, id);

        // Check whether cache image exists and created less than an hour ago
        // (create it if necessary)
        if (!File.Exists(imagePath)
            || File.GetLastWriteTime(imagePath) < DateTime.Now.AddHours(-1))
        {
            // Get the file from the web service here
            byte[] imageBytes = ...

            // Save as a file
            using (var memoryStream = new MemoryStream(imageBytes))
            using (var outputStream = File.OpenWrite(imagePath))
                Image.FromStream(memoryStream).Save(outputStream);
        }

        context.Response.ContentType = "image/png";
        context.Response.WriteFile(imagePath);
    }
于 2010-05-07T08:52:26.470 に答える