oneimageCookbook
oneimage cookbook
Answers to common image tasks in C#. Each recipe is a complete program you can run on a real image, with the output it really prints.
Resize and crop
Resize an image in C#Resize an image in C# and keep the aspect ratio: to a width, into a box, cropped to fill or padded, at the quality you choose. Runnable code with OneImage, on any OS.Create a thumbnail in C#Create a thumbnail in C# in one call: upright, never enlarged, sRGB, with EXIF and GPS removed, as JPEG or WebP, or a square that fills the box. Runnable code with OneImage.Crop an image in C#Crop an image in C#: a rectangle, a centered square, or a fixed aspect ratio with a chosen focus, in upright coordinates. Runnable code with OneImage, on any OS.Rotate or flip an image in C#Rotate an image 90 or 180 degrees, by any angle, or flip it horizontally or vertically in C#. Runnable code with OneImage, on any OS.Fix EXIF orientation in C#Phone photos come out sideways in C# because the EXIF orientation tag isn’t applied. Load them upright, save them upright, and what the eight orientation values mean. Runnable code with OneImage.Resize an animated GIF in C#Resize an animated GIF in C# and keep every frame, its delay and the loop setting. Read the frame count, make a still preview, or shrink the file as animated WebP. Runnable code with OneImage.
Convert formats
Convert PNG to JPG in C#Convert PNG to JPG in C#, keeping transparent areas white, and JPG to PNG, BMP, TIFF, GIF or ICO: the file extension picks the format. Runnable code with OneImage, on any OS.Convert an image to WebP in C#Convert JPG, PNG or GIF to WebP in C# with one line of pure .NET code, no native libwebp: quality, lossless and animated WebP. Runnable code with OneImage.Convert SVG to PNG in C#Convert SVG to PNG in C# at any size, drawn sharp rather than scaled up, with no Skia or System.Drawing: files, SVG strings and text with your own fonts. Runnable code with OneImage.Svg.Convert a PDF to an image in C#Convert PDF pages to PNG or JPG in C# with pure .NET code, no PDFium or Ghostscript: every page, one page at 300 dpi, or a thumbnail. Runnable code with OneImage.Pdf.Convert a PDF to TIFF in C#Convert a PDF to a multipage TIFF in C# with pure .NET code: every page or a range, 300 dpi, gray or black and white for archives and fax. Runnable code with OneImage.Pdf.Convert PSD to PNG in C#Convert a Photoshop PSD or PSB file to PNG, JPG or WebP in C# without Photoshop or ImageMagick, and export each layer as a PNG. Runnable code with OneImage.Psd.Split a multipage TIFF in C#Split a multipage TIFF in C# into single-page files or PNGs, count its pages without decoding, and recompress it, including fax TIFFs. Runnable code with OneImage.
Edit and compress
Compress an image in C#Compress an image in C# to reduce its file size: JPEG quality, resizing first, WebP, lossless PNG and WebP, and compressing a JPEG to a target size in KB. Runnable code with OneImage.Convert an image to grayscale in C#Convert an image to grayscale in C# with one call, with correct luminance, as a true 8-bit gray file, or to pure black and white. Runnable code with OneImage, on any OS.Add a watermark to an image in C#Add a watermark to an image in C#: a logo with opacity in a corner, or a tiled, diagonal text watermark across the photo. Runnable code with OneImage, on any OS.Draw text on an image in C#Draw text on an image in C# with a real font: position, alignment, wrapping, a background box and an outline. Runnable code with OneImage.Text, on any OS.
Image info and metadata
Get image dimensions in C#Get an image’s width, height and format in C# without loading it: read only the headers of a JPEG, PNG, GIF, WebP or TIFF, and get the displayed size of phone photos. Runnable code with OneImage.Read EXIF data in C#Read EXIF data from a photo in C#: camera make and model, date taken, orientation and the GPS position, from the headers only. Runnable code with OneImage, on any OS.Remove EXIF metadata from an image in C#Remove EXIF data, including the GPS location, from photos in C# before you publish them, keeping the photo upright and its colors right. Runnable code with OneImage.
ASP.NET Core
Resize images on the fly in ASP.NET CoreResize, crop and convert images on request in ASP.NET Core with URLs like photo.jpg?width=400: middleware with a disk cache, presets and signed URLs. Runnable code with OneImage.Web.Resize uploaded images in ASP.NET CoreResize uploaded images in ASP.NET Core: an endpoint that stores every upload upright, at most 1600 px wide, as WebP without GPS data, and crops a user-picked square. Runnable code with OneImage.