18

I would like to return an image as an ActionResult from an MVC2 controller. This image is a 1x1 white pixel (for a tracking application). I do not want to reference an image on the disk or in a database. I would like to generate the image in my method and then return it from the controller action.

Any one know how to generate a 1x1 white image that can be passed into a FileStreamResult for returning from the controller action?

tereško
  • 58,060
  • 25
  • 98
  • 150
Paul Sachs
  • 191
  • 2
  • 5
  • 2
    not that it solves your problem, but you should use a 1x1 transparent pixel in `.png` format. That way you can use it on a variety of themes. – Stephen Sep 23 '10 at 13:11

3 Answers3

20

Copied from Daniel Ballinger's FishOfPrey.com:

Response.Clear();  
string content = @"R0lGODlhAQABAPcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAP8ALAAAAAABAAEAAAgEAP8FBAA7";  
Response.ContentType = "image/gif";  
Response.BinaryWrite(System.Convert.FromBase64String(content));  
Response.End();

I don't speak C#, but if you use this string in your program, you can save storing and accessing one extra file on disk.

eumiro
  • 207,213
  • 34
  • 299
  • 261
  • 1
    I think you should perhaps include/rewrite the post content here so if your happens to die, the answer will still be useful – samy Sep 23 '10 at 13:16
  • 9
    or use `iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVQI12P4DwQACfsD/WMmxY8AAAAASUVORK5CYII=` for a shorter 1x1 png – cobbal Sep 23 '10 at 13:33
  • 3
    oh, and use `image/png` instead of `image/gif` if you use my string – cobbal Sep 23 '10 at 17:38
  • 2
    1x1 clear gif (from http://proger.i-forge.net/The_smallest_transparent_pixel/eBQ) R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw== – Jeff Widmer Mar 14 '14 at 18:51
  • 1
    From cobbal's comment above, I couldn't get his PNG snippet to work. It was returning this error: `The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.` However, I found this slightly different PNG snippet that works just fine: `iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQYV2NgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII=` – Doug S May 10 '14 at 19:14
  • 1
    Another tiny image 43 bytes: `R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==` content type: `image/gif` – gsk Sep 04 '14 at 05:48
  • I was curious how to return this from an ActionResult- and found I can just return a null for the result. – Brady Moritz Oct 04 '14 at 16:47
8

Avoid the use of Response.End();, as used in eumiro's answer, is not a good idea, read more here: http://support2.microsoft.com/kb/312629

Insted, to avoid unnecessary ThreadAbortException, change your Action to a FileContentResult like this:

public FileContentResult Track(Guid id)
{
    //do tracking stuff ....

    //return empty gif
    const string clearGif1X1 = "R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==";
    return new FileContentResult(
                       Convert.FromBase64String(clearGif1X1), "image/gif");
}

also for tracking purposes remember to add some kind of NoCache attribute

Community
  • 1
  • 1
Henrik Stenbæk
  • 3,982
  • 5
  • 31
  • 33
3

I prefer to use images with extensions so this is what I use:

    // URL is /mailers/images/pixel123.gif
    // where 123 is the tracking number
    [ActionName("images")]
    public ActionResult Pixel(string id)
    {
        int trackingID = int.Parse(id.Substring("pixel".Length, id.Length - "pixel.gif".Length));

        // do something in database

        string trackingPixel = @"R0lGODlhAQABAPcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAP8ALAAAAAABAAEAAAgEAP8FBAA7";
        return File(System.Convert.FromBase64String(trackingPixel), "image/gif");
    }
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
  • I don't know why anyone would use a GIF that is 807 bytes long. – criticabug Apr 15 '21 at 05:41
  • Looks like some better options here https://stackoverflow.com/questions/2570633/smallest-filesize-for-transparent-single-pixel-image#:~:text=The%20absolute%20smallest%20valid%20transparent%20GIF%20comes%20in%20at%2033%20bytes.&text=This%20used%20to%20be%2032,Terminator%20in%20the%20LZW%20data. – Simon_Weaver Apr 17 '21 at 02:01