Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible for a view to display a WebImage without saving it to a file first?

I'm not really sure if I'm using the WebImage class correctly.

I have a controller that pulls a photo and some related information (comments, upload date, file name) from the database. I want to return a partial view that contains this information, and display the image along with the extra info.

So I created a new WebImage from the byte array, but how do I display it?

According to this article it should be pretty simple

  1. You need to work with Razor syntax and create a variable that will contain the image:
    @{ var myImage = WebImage("/Content/myImage.jpg") // Work with the image… }

  2. Then, in order to load the image in the page, you have to show the variable that contains the image inside an HTML <img/> tag:
    <img src="@myImage"/>

Except that doesn't work, it just outputs <img src="System.Web.Helpers.WebImage"> and calling .Write doesn't help.

Is there any way to do this or do I need to split up my action into two different actions, one to return the photo information and one to return the photo itself?

like image 700
Brandon Avatar asked Jul 20 '11 17:07

Brandon


People also ask

How do I add a picture to a Visual Studio folder?

Click on the Project in Visual Studio and then click on the button titled "Show all files" on the Solution Explorer toolbar. That will show files that aren't in the project. Now you'll see that image, right click in it, and select "Include in project" and that will add the image to the project!


2 Answers

You can write it out on the fly!

You just don't use WebImage.Save() like you'd think, instead you use WebImage.GetBytes().

In this example, you've already saved an image as a byte array to the database. Simplified it a bit to only handle jpegs.

    /// <summary>
    /// Reference this in HTML as <img src="/Photo/WatermarkedImage/{ID}" />
    /// Simplistic example supporting only jpeg images.
    /// </summary>
    /// <param name="ID">Photo ID</param>
    public ActionResult WatermarkedImage(Guid ID)
    {
        // Attempt to fetch the photo record from the database using Entity Framework 4.2.
        var photo = db.Photos.Find(ID);

        if (photo != null) // Found the indicated photo record.
        {
            // Create WebImage from photo data.
            // Should have 'using System.Web.Helpers' but just to make it clear...
            var wi = new System.Web.Helpers.WebImage(photo.Data); 

            // Apply the watermark.
            wi.AddImageWatermark(Server.MapPath("~/Content/Images/Watermark.png"), 
                                 opacity: 75, 
                                 horizontalAlign: "Center", 
                                 verticalAlign: "Bottom");

            // Extract byte array.
            var image = wi.GetBytes("image/jpeg");

            // Return byte array as jpeg.
            return File(image, "image/jpeg");
        }
        else // Did not find a record with passed ID.
        {
            return null; // 'Missing image' icon will display on browser.
        }
    }
like image 159
Klom Dark Avatar answered Sep 19 '22 00:09

Klom Dark


There isn't anyway to do it in a single razor view... you need to create a seperate action to render the image.

The img tag on the page is going to make a SEPERATE http call to the server based on the url provided in the img src.

like image 34
Scrappydog Avatar answered Sep 22 '22 00:09

Scrappydog