
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to resize an Image C#?
A bitmap consists of the pixel data for a graphics image and its attributes. There are many standard formats for saving a bitmap to a file. GDI+ supports the following file formats: BMP, GIF, EXIF, JPG, PNG and TIFF. You can create images from files, streams, and other sources by using one of the Bitmap constructors and save them to a stream or to the file system with the Save method.
In the below code CompressAndSaveImageAsync Method Compresses the images and saves in the path Mentioned.
The new image name will be a combination of desktop userId and dateTime
Example
private async Task<string> CompressAndSaveImageAsync(Bitmap inputImage, int quality=50){ string imageSavedPath = string.Empty; try{ var jpgEncoder = await GetEncoderAsync(ImageFormat.Jpeg); var imageEncoder = Encoder.Quality; var imageEncoderParameters = new EncoderParameters(1); var imageEncoderParameter = new EncoderParameter(imageEncoder, quality); imageEncoderParameters.Param[0] = imageEncoderParameter; var userId = Regex.Replace(WindowsIdentity.GetCurrent().Name, @"[^0−9a−zA−Z]+", "_"); var currentDateTime = Regex.Replace(DateTimeOffset.Now.ToString().Split('+')[0].Trim(), @" [^0−9a−zA−Z]+", "_"); var imageName = $"{userId}_{currentDateTime}.jpg"; imageSavedPath = "C:\Users\K\Desktop\Questions\Images"; inputImage.Save(imageSavedPath, jpgEncoder, imageEncoderParameters); inputImage.Dispose(); } catch (Exception ex){ throw } return imageSavedPath; } private async Task<ImageCodecInfo> GetEncoderAsync(ImageFormat format){ ImageCodecInfo imageCodecInfo = null; try{ var codecs = ImageCodecInfo.GetImageDecoders(); foreach (var codec in codecs){ if (codec.FormatID == format.Guid){ imageCodecInfo = codec; } } } catch (Exception ex){ throw } return imageCodecInfo; }
- Related Articles
- How to resize an image using Tkinter?
- How to resize an image in Node Jimp?
- How to resize an image in Android using Picasso?
- How to resize an image in OpenCV using Python?
- PyTorch – How to resize an image to a given size?
- How to resize image in PHP?
- How to Resize an image in Android using Picasso on Kotlin?
- How to resize Image in Android App?
- How to auto-resize an image to fit a div container using CSS?
- How to resize Image in Android App using Kotlin?
- Resize image proportionally with CSS
- How to resize the background image to window size in Tkinter?
- How to resize an UIImageView using Swift?
- How to resize an array in Java?
- Is it possible to resize an array in C#

Advertisements