本文整理汇总了C#中System.Drawing.Imaging.EncoderParameter类的典型用法代码示例。如果您正苦于以下问题:C# EncoderParameter类的具体用法?C# EncoderParameter怎么用?C# EncoderParameter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
EncoderParameter类属于System.Drawing.Imaging命名空间,在下文中一共展示了EncoderParameter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SaveInMemory
Bitmap SaveInMemory(Stream stream, long quality, Bitmap myBitmap)
{
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;
// EncoderParameter object in the array.
using (myEncoderParameters = new EncoderParameters(1))
{
// Save the bitmap as a JPEG file with quality level.
myEncoderParameter = new EncoderParameter(myEncoder, quality);
myEncoderParameters.Param[0] = myEncoderParameter;
myBitmap.Save(stream, myImageCodecInfo, myEncoderParameters);
}
return myBitmap;
}
示例2: SaveImageForSpecifiedQuality
public bool SaveImageForSpecifiedQuality(System.Drawing.Image sourceImage, string savePath, int imageQualityValue)
{
//以下代码为保存图片时,设置压缩质量
EncoderParameters encoderParameters = new EncoderParameters();
EncoderParameter encoderParameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, imageQualityValue);
encoderParameters.Param[0] = encoderParameter;
try
{
ImageCodecInfo[] ImageCodecInfoArray = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegImageCodecInfo = null;
for (int i = 0; i < ImageCodecInfoArray.Length; i++)
{
if (ImageCodecInfoArray[i].FormatDescription.Equals("JPEG"))
{
jpegImageCodecInfo = ImageCodecInfoArray[i];
break;
}
}
sourceImage.Save(savePath, jpegImageCodecInfo, encoderParameters);
return true;
}
catch
{
return false;
}
}
示例3: CreateThumbnail
public static byte[] CreateThumbnail(byte[] PassedImage, int newWidth, int newHeight)
{
byte[] ReturnedThumbnail;
using (MemoryStream StartMemoryStream = new MemoryStream(), NewMemoryStream = new MemoryStream()) {
// write the string to the stream
StartMemoryStream.Write(PassedImage, 0, PassedImage.Length);
// create the start Bitmap from the MemoryStream that contains the image
Bitmap startBitmap = new Bitmap(StartMemoryStream);
// create a new Bitmap with dimensions for the thumbnail.
Bitmap newBitmap = new Bitmap(newWidth, newHeight);
// Copy the image from the START Bitmap into the NEW Bitmap.
// This will create a thumnail size of the same image.
newBitmap = ResizeImage(startBitmap, newWidth, newHeight);
// Save this image to the specified stream in the specified format.
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
newBitmap.Save(NewMemoryStream, jpgEncoder, myEncoderParameters);
// Fill the byte[] for the thumbnail from the new MemoryStream.
ReturnedThumbnail = NewMemoryStream.ToArray();
}
// return the resized image as a string of bytes.
return ReturnedThumbnail;
}
示例4: ImageCut
//// <summary>
/// 图片切割函数
/// </summary>
/// <param name="sourceFile">原始图片文件</param>
/// <param name="xNum">在X轴上的切割数量</param>
/// <param name="yNum">在Y轴上的切割数量</param>
/// <param name="quality">质量压缩比</param>
/// <param name="outputFile">输出文件名,不带后缀</param>
/// <returns>成功返回true,失败则返回false</returns>
public bool ImageCut(String sourceFile, int xNum, int yNum, long quality, String outputFile)
{
try
{
long imageQuality = quality;
Bitmap sourceImage = new Bitmap(sourceFile);
ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");
Encoder myEncoder = Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality);
myEncoderParameters.Param[0] = myEncoderParameter;
float xWidth = sourceImage.Width / xNum;
float yWidth = sourceImage.Height / yNum;
String outputImage = "";
for (int countY = 0; countY < yNum; countY++)
for (int countX = 0; countX < xNum; countX++)
{
RectangleF cloneRect = new RectangleF(countX * xWidth, countY * yWidth, xWidth, yWidth);
Bitmap newImage = sourceImage.Clone(cloneRect, PixelFormat.Format24bppRgb);
outputImage = outputFile + countX + countY + ".jpg";
newImage.Save(outputImage, myImageCodecInfo, myEncoderParameters);
}
return true;
}
catch
{
return false;
}
}
示例5: GetBytes
/// <summary>
/// Gets the bytes.
/// </summary>
/// <param name="image">The image.</param>
/// <param name="imageFormat">The image format.</param>
/// <param name="qualityLevel">The quality level.
/// <remarks>
/// A quality level of 0 corresponds to the greatest compression, and a quality level of 100 corresponds to the least compression.
/// https://msdn.microsoft.com/library/bb882583(v=vs.110).aspx
/// </remarks></param>
/// <returns>System.Byte[].</returns>
/// <exception cref="OperationFailureException">GetBytes</exception>
public static byte[] GetBytes(this Bitmap image, ImageFormat imageFormat = null, long qualityLevel = 100)
{
try
{
image.CheckNullObject("image");
if (imageFormat == null)
{
imageFormat = ImageFormat.Jpeg;
}
if (qualityLevel > 100 || qualityLevel < 1)
{
qualityLevel = 100;
}
using (var memoryStream = new MemoryStream())
{
var jpegEncoder = GetImageEncoder(imageFormat);
var encoder = Encoder.Quality;
var encoderParameters = new EncoderParameters(1);
var myEncoderParameter = new EncoderParameter(encoder, qualityLevel);
encoderParameters.Param[0] = myEncoderParameter;
image.Save(memoryStream, jpegEncoder, encoderParameters);
return memoryStream.ToBytes();
}
}
catch (Exception ex)
{
throw ex.Handle();
}
}
示例6: 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");
}
示例7: Main
static void Main(string[] args)
{
FileSize("noname.jpg");
var image = new Bitmap("noname.jpg");
var jgpEncoder = GetEncoder(ImageFormat.Jpeg);
var myEncoder = Encoder.Quality;
var myEncoderParameters = new EncoderParameters(1);
var myEncoderParameter = new EncoderParameter(myEncoder, 50L);
myEncoderParameters.Param[0] = myEncoderParameter;
image.Save(@"TestPhotoQualityFifty.jpg", jgpEncoder, myEncoderParameters);
FileSize("TestPhotoQualityFifty.jpg");
myEncoderParameter = new EncoderParameter(myEncoder, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;
image.Save(@"TestPhotoQualityHundred.jpg", jgpEncoder, myEncoderParameters);
FileSize("TestPhotoQualityHundred.jpg");
myEncoderParameter = new EncoderParameter(myEncoder, 70L);
myEncoderParameters.Param[0] = myEncoderParameter;
image.Save(@"TestPhotoQualityZero.jpg", jgpEncoder, myEncoderParameters);
FileSize("TestPhotoQualityZero.jpg");
Console.ReadLine();
}
示例8: StoreIcon
public static void StoreIcon(string extension, string path, string mimeType)
{
if (!File.Exists(path))
{
try
{
if (!Directory.Exists(Path.GetDirectoryName(path)))
Directory.CreateDirectory(Path.GetDirectoryName(path));
Bitmap image = IconHandler.IconFromExtension(extension, IconSize.Small);
System.Drawing.Imaging.Encoder qualityEncoder = System.Drawing.Imaging.Encoder.Quality;
long quality = 100;
EncoderParameter ratio = new EncoderParameter(qualityEncoder, quality);
EncoderParameters codecParams = new EncoderParameters(1);
codecParams.Param[0] = ratio;
ImageCodecInfo mimeCodecInfo = null;
foreach (ImageCodecInfo codecInfo in ImageCodecInfo.GetImageEncoders())
{
if (codecInfo.MimeType == mimeType)
{
mimeCodecInfo = codecInfo;
break;
}
}
if (mimeCodecInfo != null)
image.Save(path, mimeCodecInfo, codecParams); // Save to JPG
}
catch (Exception ex)
{
logger.LogException(LogLevel.Error, "Failed to create icon", ex);
}
}
}
示例9: convertToPng
private void convertToPng(string sourcedir, string sourceFile, string sourceDirandFile)
{
try
{
// Load the image.
System.Drawing.Image image1 = System.Drawing.Image.FromFile(sourceDirandFile);
if (filetype == "jpg")
{
ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
// Create an Encoder object based on the GUID
// for the Quality parameter category.
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;
image1.Save(sourcedir + "\\" + sourceFile + ".jpg", jgpEncoder, myEncoderParameters);
}
else if (filetype == "png")
{
// Save the image in PNG format.
image1.Save(sourcedir + "\\" + sourceFile + ".png", System.Drawing.Imaging.ImageFormat.Png);
}
progressBar1.Increment(1);
}
catch
{
Console.WriteLine("Crap");
}
}
示例10: CropAndResize
public static bool CropAndResize(string sourceFilePath, string targetFilePath, int x, int y, int width, int height, int destWidth=0, int destHeight=0 )
{
Image img = getImage(sourceFilePath, targetFilePath);
Image target = ImageCropAndResize(img, x, y, width, height, destWidth, destHeight);
img.Dispose();
String extension = System.IO.Path.GetExtension(sourceFilePath).ToLower();
if (extension != ".png")
{
ImageCodecInfo jgpEncoder = ImageTools.GetJpgCodec();
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 98L);
myEncoderParameters.Param[0] = myEncoderParameter;
target.Save(targetFilePath, jgpEncoder, myEncoderParameters);
}
else
{
target.Save(targetFilePath, System.Drawing.Imaging.ImageFormat.Png);
}
return true;
}
示例11: BmpSave
public static void BmpSave(Bitmap newBmp, string outFile)
{
EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[1];
quality[0] = 100;
EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICI = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[x];//设置JPEG编码
break;
}
}
if (jpegICI != null)
{
newBmp.Save(outFile, jpegICI, encoderParams);
}
else
{
newBmp.Save(outFile, ImageFormat.Jpeg);
}
newBmp.Dispose();
}
示例12: KiSaveAsJPEG
public static bool KiSaveAsJPEG(Bitmap bmp, string FileName, int Qty)
{
try
{
EncoderParameter p;
EncoderParameters ps;
ps = new EncoderParameters(1);
p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, Qty);
ps.Param[0] = p;
try
{
File.Delete(FileName);
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
bmp.Save(FileName, GetCodecInfo("image/jpeg"), ps);
return true;
}
catch
{
return false;
}
}
示例13: work
public static void work() {
keepGoing = true;
Task.Factory.StartNew(()=>check());
do {
Bitmap img = GetScreenShot();
ImageCodecInfo[] codec = ImageCodecInfo.GetImageEncoders();
// initialisation d'un tableau de dimension 2 de parametres d'encodages
EncoderParameters encodeurs = new EncoderParameters(2);
// parametre qualite
EncoderParameter qualite = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality,quality);
// Parametre compression
EncoderParameter compression = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, compress);
// remplir tableau parametre
encodeurs.Param[0] = qualite;
encodeurs.Param[1] = compression;
using (MemoryStream stm = new MemoryStream())
{
img.Save(stm, codec[1], encodeurs);
if (Cnx.sendArray(stm.ToArray(), "04")) { Console.WriteLine("success "); }
}
Thread.Sleep(sleeptime);
} while (keepGoing);
}
示例14: 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)
{
}
}
示例15: 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;
}