user1520494
user1520494

Reputation: 1184

Get an image from web content folder outside web project

The objective

I have 2 projects at the same solution and the objective is to Get an image from web project on domain project.

  1. Project.web ("web application") "here's the image /Content/Images"
  2. Project.domain ("All domain entities") "I want to take it from here!!"

The problem
I was trying to read from my Web "Content/Images" folder but I don't know how to set the url and if that's possible to do.

The ugly and dirty code that works is this: but I don't want to fix the image on a folder>

Image logo = Image.FromFile(@"c:\folder\img.png");

Upvotes: 1

Views: 2441

Answers (2)

Oleg Grabets
Oleg Grabets

Reputation: 51

I spent some time searching for Server variable, enjoy! :)

public async Task<IHttpActionResult> GetRenderImage(long id, int size = 0)
    {
        // Load template           
        var file = HttpContext.Current.Server.MapPath("~/Content/media/template.png"); 
        WebImage img = new WebImage(file);

        // some operations with image, for sample scale or crop
        ...

        // get image data
        var bytes = img.GetBytes("image/jpeg");

        // Save to blob
        var p = getCloudBlobContainer("RenderImg");
        CloudBlockBlob blob = p.GetBlockBlobReference(String.Format("{0}.jpg", id);
        blob.Properties.ContentType = "image/jpeg";

        await blob.UploadFromByteArrayAsync(bytes, 0, bytes.Length);
        var url = blob.Uri.ToString();

        // Redirect to Azure blob image
        return Redirect(url);
    }

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1038810

You realize that when you deploy your application in IIS there's no longer a notion of other projects. So you should include this image as part of your ASP.NET application. You could have for example a post-build compilation step which copies the image from your Project.domain to your ASP.NET MVC application (for example inside the App_Data folder) so that inside your MVC application you will be able to use the Server.MapPath method to access the image:

using (var image = Image.FromFile(Server.MapPath("~/App_Data/img.png")))
{
    ...
}

Upvotes: 1

Related Questions