2

このリンクを参考に、 MessagingToolkit.QRCode.dll を使って QR コード画像を作成しました。保存した画像をmyLayoutで同じビューに表示する方法。
コントローラーコード

  [HttpPost]
    public ActionResult GenerateExcelQR(string Command, FormCollection collection)
    {
    if (Command == "Excel")
    {
    //  logic to generate Excel
    }
    else if(Command =="QRCode")
     // qr code logic //
        QRCodeEncoder encoder = new QRCodeEncoder();
        Bitmap img = encoder.Encode(qrcode);
        string path = Server.MapPath("/Images/QRCode.jpg"); 
        img.Save(path, ImageFormat.Jpeg);
        return base.File(path,"image/jpeg"); // Displays the QR image without my layout.
     // return View(path,"image/jpeg");      // Returns an error specifying "Images\QRCode.jpg' or its master was not found or no view engine supports the searched locations."
    }

レイアウトと同じビューに QRCode 画像を表示する方法。
助言がありますか。
ImageModel.cs の編集

public static class ImageModel
    {
        public static string Image(this HtmlHelper htmlHelper, string path, string alt)
        {
            var img = new TagBuilder("img");
            img.MergeAttribute("src", path);
            img.MergeAttribute("alt", alt);
            return img.ToString(TagRenderMode.SelfClosing);
        }
    }

ビューで

@model IEnumerable<SampleECommerce.Models.CheckoutModel> // reference to 1st model to return values
@using SampleECommerce.Models; // for Imagemodel
4

1 に答える 1

1

ファイルではなくビューを返し、ビュー内で画像をレンダリングする必要があります。ここで Image - は html ヘルパーです:

public static HtmlString Image(this HtmlHelper helper, string path, string alt)
{
    var img = new TagBuilder("img");
    img.MergeAttribute("src", path);
    img.MergeAttribute("alt", alt);
    return new HtmlString(img.ToString(TagRenderMode.SelfClosing));
}

そして、それをビューで使用するだけです:

@Html.Image(path_to_your_image, "Description")

そして、あなたのコントローラではちょうどreturn View();

このヘルパーを使用するには、プロジェクト内に「Helpers」などのフォルダーを作成する必要があります。次に、このフォルダーにファイルを作成します (たとえば、'HtmlImageHelper.cs')。このファイルにヘルパーのコンテンツを入れます。この後、上部のビューで次のように記述しますusing YourProjectName.Helpers。この後、このhtmlヘルパーを使用して画像をレンダリングできます

于 2013-04-03T10:09:21.343 に答える