Creating website thumbnail images in ASP.NET
Following on from my post about generating PDF thumbnails in ASP.NET, here is another one about generating website thumbnail images using DynamicImage.
<sitdap:DynamicImage runat="server" ImageFormat="Jpeg"> <Layers> <sitdap:WebsiteScreenshotLayer WebsiteUrl="http://www.microsoft.com"> <Filters> <sitdap:ResizeFilter Width="500" Mode="UseWidth" /> </Filters> </sitdap:WebsiteScreenshotLayer> </Layers> </sitdap:DynamicImage>
You can also use the fluent interface:
string imageUrl = new DynamicImageBuilder() .WithLayer( new WebsiteScreenshotLayerBuilder().WebsiteUrl("http://www.microsoft.com") .WithFilter(FilterBuilder.Resize.ToWidth(500)) ).Url;
The preceding code will result in this image:
![]()
As you can see, the image is of the whole web page, not just the cropped window you'd normally see. If you want that, you can use the Crop filter:
<sitdap:DynamicImage runat="server" ImageFormat="Jpeg"> <Layers> <sitdap:WebsiteScreenshotLayer WebsiteUrl="http://www.microsoft.com"> <Filters> <sitdap:ResizeFilter Width="500" Mode="UseWidth" /> <sitdap:CropFilter Width="500" Height="500" /> </Filters> </sitdap:WebsiteScreenshotLayer> </Layers> </sitdap:DynamicImage>
Now you should have:
![]()
You can optionally specify a timeout, after which if the screenshot capture process hasn't finished, it will terminate and not produce an image.
<sitdap:WebsiteScreenshotLayer WebsiteUrl="http://www.microsoft.com" Timeout="3000" />
new WebsiteScreenshotLayerBuilder() .WebsiteUrl("http://www.microsoft.com") .Timeout(3000)
Since these images will take a while to produce, you'll probably want to enable caching in DynamicImage.
Thanks are due to to 'NoLoveLust's work on wrapping CutyCapt.exe, which provided a starting point for this code.
To get it working, you'll need to copy CutyCapt.exe from the Tools folder in the repo to App_Data/DynamicImage.
2 comments
Jun 3, 2011
16:09
Hey,
Where can i get this code working?
I tried github and ran the msbuild and then it simply doesnt exist.. :P
Jun 7, 2011
16:18
@Jorge The easiest way to get it working is to install the NuGet package (it's called DynamicImage.Extensions.ContentAwareResizing).
If that's not an option, then you'll have to do it the old-fashioned way: * Open the Visual Studio solution (SoundInTheory.DynamicImage.Extensions.sln). * Build it, which gives you the .NET DLLS * Copy CAIR.exe and pthreadVSE2.dll (from build/NuGet/DynamicImage.Extensions.ContentAwareResizing/content/App_Data/DynamicImage) into your App_Data/DynamicImage folder.
Hope that helps.
Make a comment
Sorry, commenting has been temporary disabled because of spam. If you have any questions, you can email me, and you can also find me on Twitter.