AceInfinity
Emeritus, Contributor
I've been exploring more documentation today, and there hasn't been much on Imaging yet here, so I thought I'd post an example on how to utilize an ImageCodec and Encoders to transform an image and save it back to the filesystem. In my example, I save/create a backup just in case, for convenience.
Note: ImgEncoder is my own definition...
Some valid MimeTypes are listed below:
:wave:
Code:
private unsafe void MainMethod()
{
FlipImgHorizontal(@"Z:\IMG\girl.jpg", "image/jpeg");
}
private void FlipImgHorizontal(string fileName, string mimeType)
{
ImageCodecInfo imgCodec;
ImgEncoder enc;
string bakupFile = fileName + ".bak";
File.Copy(fileName, bakupFile, true);
imgCodec = ImageCodecInfo.GetImageEncoders().FirstOrDefault(e => e.MimeType == mimeType);
if (imgCodec == null)
throw new ArgumentException("Invalid MimeType specified.");
enc = ImgEncoder.Transformation;
using (Bitmap bmp = new Bitmap(bakupFile))
{
using (EncoderParameters encParams = new EncoderParameters(1))
{
using (EncoderParameter encParam = new EncoderParameter(enc, (long)EncoderValue.TransformFlipHorizontal))
{
encParams.Param[0] = encParam;
bmp.Save(fileName, imgCodec, encParams);
}
}
}
}
Note: ImgEncoder is my own definition...
Code:
using BitmapEncoder = System.Drawing.Imaging.Encoder;
Some valid MimeTypes are listed below:
Code:
image/bmp
image/jpeg
image/gif
image/tiff
image/png
:wave: