本文整理汇总了C#中System.Drawing.Imaging.EncoderParameters类的典型用法代码示例。如果您正苦于以下问题:C# EncoderParameters类的具体用法?C# EncoderParameters怎么用?C# EncoderParameters使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
EncoderParameters类属于System.Drawing.Imaging命名空间,在下文中一共展示了EncoderParameters类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Resize
public static void Resize(this Image source, String newFilename, Size newSize, long quality, ContentAlignment contentAlignment, ThumbMode mode)
{
Image image = source.Resize(newSize, quality, contentAlignment, mode);
using (EncoderParameters encoderParams = new EncoderParameters(1))
{
using (EncoderParameter parameter = (encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, quality)))
{
ImageCodecInfo encoder = null;
//取得擴展名
string ext = Path.GetExtension(newFilename);
if (string.IsNullOrEmpty(ext))
ext = ".jpg";
//根據擴展名得到解碼、編碼器
foreach (ImageCodecInfo codecInfo in ImageCodecInfo.GetImageEncoders())
{
if (Regex.IsMatch(codecInfo.FilenameExtension, string.Format(@"(;|^)\*\{0}(;|$)", ext), RegexOptions.IgnoreCase))
{
encoder = codecInfo;
break;
}
}
DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(newFilename));
if(dir.Exists == false) dir.Create();
image.Save(newFilename, encoder, encoderParams);
}
}
}
示例2: ImageFromGDIPlus
//http://social.msdn.microsoft.com/forums/en-US/wpf/thread/0f037b9c-779d-45ad-b797-01c25999491b
public static BitmapImage ImageFromGDIPlus(byte[] bytes)
{
var guid = Guid.NewGuid();
if (Directory.Exists(@"C:\Temp\") == false)
Directory.CreateDirectory(@"C:\Temp\");
var stream = new MemoryStream(bytes);
var badMetadataImage = new Bitmap(stream);
ImageCodecInfo myImageCodecInfo;
Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
// get an ImageCodecInfo object that represents the JPEG codec
myImageCodecInfo = GetEncoderInfo("image/jpeg");
// Create an Encoder object based on the GUID for the Quality parameter category
myEncoder = Encoder.Quality;
// Create an EncoderParameters object
// An EncoderParameters object has an array of EncoderParameter objects.
// In this case, there is only one EncoderParameter object in the array.
myEncoderParameters = new EncoderParameters(1);
// Save the image as a JPEG file with quality level 75.
myEncoderParameter = new EncoderParameter(myEncoder, 75L);
myEncoderParameters.Param[0] = myEncoderParameter;
badMetadataImage.Save(@"C:\Temp\" + guid + ".bmp", myImageCodecInfo, myEncoderParameters);
// Create the source to use as the tb source
var bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(@"C:\Temp\" + guid + ".bmp", UriKind.Absolute);
bi.EndInit();
return bi;
}
示例3: CompressImage
public static void CompressImage(Image sourceImage, int imageQuality, string savePath)
{
try
{
//Create an ImageCodecInfo-object for the codec information
ImageCodecInfo jpegCodec = null;
//Set quality factor for compression
EncoderParameter imageQualitysParameter = new EncoderParameter(
System.Drawing.Imaging.Encoder.Quality, imageQuality);
//List all avaible codecs (system wide)
ImageCodecInfo[] alleCodecs = ImageCodecInfo.GetImageEncoders();
EncoderParameters codecParameter = new EncoderParameters(1);
codecParameter.Param[0] = imageQualitysParameter;
//Find and choose JPEG codec
for (int i = 0; i < alleCodecs.Length; i++)
{
if (alleCodecs[i].MimeType == "image/jpeg")
{
jpegCodec = alleCodecs[i];
break;
}
}
//Save compressed image
sourceImage.Save(HttpContext.Current.Server.MapPath(savePath), jpegCodec, codecParameter);
}
catch (Exception ex)
{
}
}
示例4: Main
/// <summary>
/// Takes a screen-size + internal bezel width sized bitmap and crops it to remove the area taken up by the bezels. Making the image appear as if it was seen through a window.
/// </summary>
/// <param name="filePaths">The file paths.</param>
/// <remarks>
/// Writes a file of the same name with -wallpapered appended to the end in JPEG format to the same location as the source.
/// </remarks>
public static void Main(String[] filePaths)
{
if(filePaths.Length < 1)
{
DisplayInstructions();
return;
}
foreach(var filePath in filePaths)
{
String fileName = Path.GetFileNameWithoutExtension(filePath);
String fileDirectory = Path.GetDirectoryName(filePath);
String jpgFilePath = String.Format("{0}{1}{2}-wallpapered.jpg", fileDirectory, Path.DirectorySeparatorChar, fileName);
using(Bitmap sourceBitmap = new Bitmap(filePath))
{
//Check size
if(sourceBitmap.Width != TotalMonitorWidth || sourceBitmap.Height != MaximumMonitorHeight)
{
DisplayInstructions();
continue;
}
Bitmap wallpaper = CreateWallpaper(sourceBitmap);
//Save as JPEG
var jpegImageCodecInfo = GetEncoderInfo("image/jpeg");
var qualityEncoder = Encoder.Quality;
var encoderParameters = new EncoderParameters(1);
var encoderParameter = new EncoderParameter(qualityEncoder, (Int64)JPEGQuality);
encoderParameters.Param[0] = encoderParameter;
wallpaper.Save(jpgFilePath, jpegImageCodecInfo, encoderParameters);
}
}
}
示例5: GenerateThumbnail
private static void GenerateThumbnail(HttpPostedFileBase file, string rootFolder, string fileName)
{
using (var image = Image.FromStream(file.InputStream, true, true)) /* Creates Image from specified data stream */
{
using (var thumb = image.GetThumbnailImage(
100, /* width*/
100, /* height*/
() => false,
IntPtr.Zero))
{
var jpgInfo = ImageCodecInfo.GetImageEncoders().Where(codecInfo => codecInfo.MimeType == "image/png").First(); /* Returns array of image encoder objects built into GDI+ */
using (var encParams = new EncoderParameters(1))
{
var appDataThumbnailPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, rootFolder, "thumbnails");
if (!Directory.Exists(appDataThumbnailPath))
{
Directory.CreateDirectory(appDataThumbnailPath);
}
string outputPath = Path.Combine(appDataThumbnailPath, "thumb_" + fileName);
long quality = 100;
encParams.Param[0] = new EncoderParameter(Encoder.Quality, quality);
thumb.Save(outputPath, jpgInfo, encParams);
}
}
}
}
示例6: ConvertImages
static async Task ConvertImages()
{
var folders = new List<string> {
@"day-1",
@"day-2",
@"day-3",
@"day-4",
@"day-5",
@"day-6"
};
//Test
var testImg = Guid.Parse("13911e38-af31-48eb-a39d-b5e695124351");
var allFiles = folders
.Select(folder => ExtractImageMetaData(folder))
.SelectMany(img => img)
//.Where(f => f.Id == testImg)
.ToArray();
var numFiles = allFiles.Count();
const long quality = 100L;
var qualityParam = System.Drawing.Imaging.Encoder.Quality;
var jpgEncoder = ImageCodecInfo.GetImageDecoders().First(enc => enc.FormatID == ImageFormat.Jpeg.Guid);
var parameters = new EncoderParameters(1);
parameters.Param[0] = new EncoderParameter(qualityParam, quality);
await Task.WhenAll(allFiles
//.Take(100)
.Select(file => CopyImage(file, jpgEncoder, parameters)));
Debugger.Break();
}
示例7: SaveJpeg
/// <summary>
/// 保存到Jpeg图片,且可以设置压缩质量
/// </summary>
/// <param name="image"></param>
/// <param name="filename">保存路径</param>
/// <param name="quality">压缩质量</param>
public static void SaveJpeg(this Image image, string filename, long quality) {
var encoder = ImageCodecInfo.GetImageEncoders().First(
c => c.FormatID == ImageFormat.Jpeg.Guid);
var parameters = new EncoderParameters();
parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
image.Save(filename, encoder, parameters);
}
示例8: SavePNG
/// <summary>
/// Saves an image as a jpeg image, with the given quality
/// </summary>
/// <param name="path">Path to which the image would be saved.</param>
/// <param name="quality">An integer from 0 to 100, with 100 being the
/// highest quality</param>
/// <exception cref="ArgumentOutOfRangeException">
/// An invalid value was entered for image quality.
/// </exception>
public void SavePNG(Image[] images, int quality, string workspace)
{
//ensure the quality is within the correct range
if ((quality < 0) || (quality > 100))
{
//create the error message
string error = string.Format("Jpeg image quality must be between 0 and 100, with 100 being the highest quality. A value of {0} was specified.", quality);
//throw a helpful exception
throw new ArgumentOutOfRangeException(error);
}
//create an encoder parameter for the image quality
var qualityParam = new EncoderParameter(Encoder.Quality, quality);
//get the jpeg codec
ImageCodecInfo jpegCodec = converter.GetEncoderInfo("image/png");
//create a collection of all parameters that we will pass to the encoder
var encoderParams = new EncoderParameters(1);
//set the quality parameter for the codec
encoderParams.Param[0] = qualityParam;
//save the images using the codec and the parameters
//each image will be saved into it's folder
for (int i = 0; i < images.Length; i++)
{
for (int j = 0; j < dirs.Length; j++)
{
string desination = (path + splitter + workspace + dirs[i]);
Check(desination);
images[i].Save(desination, jpegCodec, encoderParams);
}
}
}
示例9: CreateThumnail
public void CreateThumnail(HttpPostedFileBase file, string folderPath, string fileSuffix)
{
using (var image = Image.FromStream(file.InputStream, true, true)) /* Creates Image from specified data stream */
{
using (var thumb = image.GetThumbnailImage(
36, /* width*/
30, /* height*/
() => false,
IntPtr.Zero))
{
var jpgInfo = ImageCodecInfo.GetImageEncoders().Where(codecInfo => codecInfo.MimeType == "image/png").First(); /* Returns array of image encoder objects built into GDI+ */
using (var encParams = new EncoderParameters(1))
{
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
//Change file extention to thumb
var fileName = Path.ChangeExtension(file.FileName, "thumb");
string outputPath = Path.Combine(folderPath, GetFileNameWithSuffix(fileName, fileSuffix));
long quality = 100;
encParams.Param[0] = new EncoderParameter(Encoder.Quality, quality);
thumb.Save(outputPath, jpgInfo, encParams);
//thumb.Save(Path.ChangeExtension(file.FileName, "thumb"))
}
}
}
}
示例10: BaseCreateResponsive
public void BaseCreateResponsive(ImageCreateResponsive method)
{
var mediaType = new MediaTypes(this.Strategy.Session).Jpeg;
byte[] content;
// Stream should be left open for Save to work
using (Stream stream = new MemoryStream(this.Original.Content))
{
var responsive = new Bitmap(stream);
responsive = responsive.Rotate();
responsive = responsive.MaxHeight(method.MaxHeight ?? 600);
var encoder = ImageCodecInfo.GetImageEncoders().FirstOrDefault(e => e.MimeType == mediaType.Name);
var encoderParams = new EncoderParameters(1);
var qualityParam = Encoder.Quality;
encoderParams.Param[0] = new EncoderParameter(qualityParam, 72L);
content = responsive.Save(encoder, encoderParams);
}
if (!this.ExistResponsive || !content.SequenceEqual(this.Responsive.Content))
{
if (this.ExistResponsive)
{
this.Responsive.Delete();
}
this.Responsive = new MediaBuilder(this.Strategy.Session).WithContent(content).WithMediaType(mediaType).Build();
}
}
示例11: Thumbnail
public ActionResult Thumbnail(string path)
{
var myCallback =
new Image.GetThumbnailImageAbort(ThumbnailCallback);
var paths = new List<string>(2);
BuildPath(path, out folderPath, out resourcePath);
var folder = session.OpenFolder(folderPath + "/");
var resource = folder.GetResource(resourcePath + "/");
var sourceStream = resource.GetReadStream();
Bitmap bitmap = null;
try
{
bitmap = new Bitmap(sourceStream);
}
catch (Exception)
{
var fs = new FileStream(Server.MapPath("~/Content/kendo/2014.2.716/Bootstrap/imagebrowser.png"), FileMode.Open);
var tempBs = new byte[fs.Length];
fs.Read(tempBs, 0, tempBs.Length);
return new FileContentResult(tempBs, "image/jpeg");
}
var myThumbnail = bitmap.GetThumbnailImage(84, 70, myCallback, IntPtr.Zero);
var ms = new MemoryStream();
var myEncoderParameters = new EncoderParameters(1);
var myEncoderParameter = new EncoderParameter(Encoder.Quality, 25L);
myEncoderParameters.Param[0] = myEncoderParameter;
myThumbnail.Save(ms, GetEncoderInfo("image/jpeg"), myEncoderParameters);
ms.Position = 0;
var bytes = ms.ToArray();
return new FileContentResult(bytes, "image/jpeg");
}
示例12: Capture
public byte[] Capture()
{
if (_camDriver.IsVideoStarted() == false)
{
_camDriver.StartVideo();
}
// wait for the camera to capture something
//if (_camDriver.Capture())
//{
_camEvent.WaitOne();
if (_captureStream == null)
{
_captureStream = new MemoryStream();
}
ImageCodecInfo jpgEncoder = ImageCodecInfo.GetImageEncoders().Single(x => x.FormatDescription == "JPEG");
System.Drawing.Imaging.Encoder encoder2 = System.Drawing.Imaging.Encoder.Quality;
EncoderParameters parameters = new System.Drawing.Imaging.EncoderParameters(1);
EncoderParameter parameter = new EncoderParameter(encoder2, 50L);
parameters.Param[0] = parameter;
_captureStream.Seek(0, SeekOrigin.Begin);
_camImage.Save(_captureStream, jpgEncoder, parameters);
//_camImage.Save(_captureStream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] buffer = new byte[_captureStream.Length];
Console.WriteLine("Sending image with size " + buffer.Length);
//_captureStream.Read(buffer,0,(int)_captureStream.Length);
buffer = _captureStream.ToArray();
return buffer;
//return b;
}
示例13: ResizeImage
public static Stream ResizeImage(Stream originalImage, int canvasWidth, int canvasHeight)
{
using (Image img = Image.FromStream(originalImage))
{
Image resizedImage = ResizeImage(img, canvasWidth, canvasHeight);
System.Drawing.Imaging.ImageCodecInfo info = ImageCodecInfo.GetImageEncoders()
.Where(codec => String.Compare(codec.FilenameExtension, "*.png", true) == 0)
.FirstOrDefault();
//MemoryStream stream = new MemoryStream();
MemoryStream stream = new MemoryStream();
if (info != null)
{
EncoderParameters encoderParameters;
encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality,
100L);
resizedImage.Save(stream, info, encoderParameters);
}
return stream;
}
}
示例14: GetBytes
public static byte[] GetBytes(this Bitmap bitmap, OutputFormat outputFormat)
{
using (var memStream = new MemoryStream())
{
bitmap.MakeTransparent(BackgroundColour);
switch (outputFormat)
{
case OutputFormat.Jpeg:
bitmap.Save(memStream, ImageFormat.Jpeg);
break;
case OutputFormat.Gif:
bitmap.Save(memStream, ImageFormat.Gif);
break;
case OutputFormat.Png:
bitmap.Save(memStream, ImageFormat.Png);
break;
case OutputFormat.HighQualityJpeg:
var p = new EncoderParameters(1);
p.Param[0] = new EncoderParameter(Encoder.Quality, (long)95);
bitmap.Save(memStream, GetImageCodeInfo("image/jpeg"), p);
break;
}
return memStream.ToArray();
}
}
示例15: ConvertTiffToBitmap
/// <summary>
/// Convert Tiff image to another mime-type bitmap
/// </summary>
/// <param name="tiffImage">Source TIFF file</param>
/// <param name="mimeType">Desired result mime-type</param>
/// <returns>Converted image</returns>
public Bitmap ConvertTiffToBitmap(Image tiffImage, string mimeType)
{
var imageCodecInfo = ImageCodecInfo.GetImageEncoders().FirstOrDefault(encoder => encoder.MimeType == "image/tiff");
if (imageCodecInfo == null)
{
return null;
}
Bitmap sourceImg;
using (var memoryStream = new MemoryStream())
{
// Setting encode params
var imageEncoderParams = new EncoderParameters(1);
imageEncoderParams.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
tiffImage.Save(memoryStream, imageCodecInfo, imageEncoderParams);
tiffImage.Dispose();
var ic = new ImageConverter();
// Reading stream data to new image
var tempTiffImage = (Image)ic.ConvertFrom(memoryStream.GetBuffer());
// Setting new result mime-type
imageCodecInfo = ImageCodecInfo.GetImageEncoders().FirstOrDefault(encoder => encoder.MimeType == mimeType);
if (tempTiffImage != null) tempTiffImage.Save(memoryStream, imageCodecInfo, imageEncoderParams);
sourceImg = new Bitmap(Image.FromStream(memoryStream, true));
}
return sourceImg;
}