本文整理汇总了C#中Bitmap.SetPropertyItem方法的典型用法代码示例。如果您正苦于以下问题:C# Bitmap.SetPropertyItem方法的具体用法?C# Bitmap.SetPropertyItem怎么用?C# Bitmap.SetPropertyItem使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Bitmap
的用法示例。
在下文中一共展示了Bitmap.SetPropertyItem方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ScaleBitmap
public Bitmap ScaleBitmap(Bitmap Bitmap, int scaleWidth, int scaleHeight)
{
Bitmap scaledBitmap;
try
{
// int scaleWidth = (int)Math.Max(Bitmap.Width * ScaleFactorX, 1.0f);
// int scaleHeight = (int)Math.Max(Bitmap.Height * ScaleFactorY, 1.0f);
scaledBitmap = new Bitmap(scaleWidth, scaleHeight);
// Scale the bitmap in high quality mode.
using (Graphics gr = Graphics.FromImage(scaledBitmap))
{
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gr.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gr.DrawImage(Bitmap, new Rectangle(0, 0, scaleWidth, scaleHeight), new Rectangle(0, 0, Bitmap.Width, Bitmap.Height), GraphicsUnit.Pixel);
}
// Copy original Bitmap's EXIF tags to new bitmap.
foreach (PropertyItem propertyItem in Bitmap.PropertyItems)
{
scaledBitmap.SetPropertyItem(propertyItem);
}
}
catch (Exception ex)
{
scaledBitmap = null;
}
return scaledBitmap;
}
示例2: AddThumbnail
public static void AddThumbnail(string imagePath, int width, string fileNameAddition, string fileSavePath)
{
bool renameImage = true;
string savePath = "";
try
{
System.IO.Stream imageStream = File.Open(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read);
try
{
string imageNameWithoutExt = System.IO.Path.GetFileNameWithoutExtension(imagePath);
savePath = fileSavePath.TrimEnd('\\') + "\\" + imageNameWithoutExt;
savePath += fileNameAddition;
savePath += ".jpg";
if (!File.Exists(savePath))
{
System.Drawing.Image mg = System.Drawing.Image.FromStream(imageStream, true);
double multiplier = (double)width / mg.Width;
int newWidth = width;
int newHeight = (int)(mg.Height * multiplier);
Size newSize = new Size(newWidth, newHeight);
Bitmap bp = new Bitmap(newSize.Width, newSize.Height);
Graphics g = Graphics.FromImage(bp);
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
Rectangle rect = new Rectangle(0, 0, newSize.Width, newSize.Height);
g.DrawImage(mg, rect, 0, 0, mg.Width, mg.Height, GraphicsUnit.Pixel);
foreach (PropertyItem pItem in mg.PropertyItems)
{
bp.SetPropertyItem(pItem);
}
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo codec = null;
for (int i = 0; i < codecs.Length; i++)
{
if (codecs[i].MimeType.Equals("image/jpeg"))
codec = codecs[i];
}
if (codec != null)
{
EncoderParameters encoderParameters = new EncoderParameters(2);
System.Drawing.Imaging.Encoder encoder = System.Drawing.Imaging.Encoder.Quality;
EncoderParameter encoderParameter = new EncoderParameter(encoder, 95L);
encoderParameters.Param[0] = encoderParameter;
encoder = System.Drawing.Imaging.Encoder.ColorDepth;
encoderParameter = new EncoderParameter(encoder, 100L);
encoderParameters.Param[1] = encoderParameter;
bp.Save(savePath, codec, encoderParameters);
}
}
}
catch
{
renameImage = false;
}
finally
{
imageStream.Close();
}
}
catch
{
//Could not open image
}
//if (insituResize && renameImage && savePath != "")
//{
// System.IO.File.Delete(imagePath);
// System.IO.File.Move(savePath, imagePath);
//}
}
示例3: SaveAndResizeImage
public static IFileObject SaveAndResizeImage(Stream input, IFileSystem fs, IFileObject file, int maxWidth, int maxHeight, ImageFormat saveAsFormat )
{
Image img;
MemoryStream imgSrcStream = new MemoryStream();
byte[] buf = new byte[1024*50];
int bufRead = 0;
do {
bufRead = input.Read(buf, 0, buf.Length);
if (bufRead>0)
imgSrcStream.Write(buf, 0, bufRead);
} while (bufRead>0);
imgSrcStream.Position = 0;
try {
img = Image.FromStream(imgSrcStream);
} catch (Exception ex) {
throw new Exception( WebManager.GetLabel("Invalid image format") );
}
if (img.Size.Width==0 || img.Size.Height==0)
throw new Exception( WebManager.GetLabel("Invalid image size") );
var sizeIsWidthOk = (maxWidth<=0 || img.Size.Width<=maxWidth);
var sizeIsHeightOk = (maxHeight<=0 || img.Size.Height<=maxHeight);
var sizeIsOk = sizeIsWidthOk && sizeIsHeightOk;
var originalImgFmt = ResolveImageFormat( Path.GetExtension(file.Name) ) ?? ImageFormat.Bmp;
var formatIsOk = (saveAsFormat==null && !originalImgFmt.Equals(ImageFormat.Bmp) && !originalImgFmt.Equals(ImageFormat.Tiff) ) || originalImgFmt.Equals(saveAsFormat);
if (!formatIsOk || !sizeIsOk ) {
var saveAsFormatResolved = saveAsFormat!=null ? saveAsFormat : (originalImgFmt==ImageFormat.Jpeg?ImageFormat.Jpeg:ImageFormat.Png);
var newFmtExtension = GetImageFormatExtension(saveAsFormatResolved);
var newFile = fs.ResolveFile( file.Name + (Path.GetExtension(file.Name).ToLower()==newFmtExtension ? String.Empty : newFmtExtension) );
newFile.CreateFile();
if (!sizeIsOk) {
var newWidth = img.Size.Width;
var newHeight = img.Size.Height;
if (!sizeIsWidthOk) {
newWidth = maxWidth;
newHeight = (int) Math.Floor( ((double)img.Size.Height)*( ((double)maxWidth)/((double)img.Size.Width) ) );
if ( maxHeight<0 || newHeight<=maxHeight )
sizeIsHeightOk = true;
}
if (!sizeIsHeightOk) {
newHeight = maxHeight;
newWidth = (int) Math.Floor( ((double)img.Size.Width)*( ((double)maxHeight)/((double)img.Size.Height) ) );
}
var resizedBitmap = new Bitmap(img, newWidth, newHeight);
var imageProps = img.PropertyItems;
foreach (PropertyItem propItem in imageProps){
resizedBitmap.SetPropertyItem(propItem);
}
SaveImage(resizedBitmap, newFile.GetContent().OutputStream, saveAsFormatResolved);
} else {
SaveImage(img, newFile.GetContent().OutputStream, saveAsFormatResolved );
}
newFile.Close();
return newFile;
}
file.CreateFile();
imgSrcStream.Position = 0;
file.CopyFrom( imgSrcStream );
file.Close();
return file;
}
示例4: SetCreationTime
public static void SetCreationTime(
Bitmap i,
DateTime creationTime)
{
try
{
PropertyItem piDate = i.GetPropertyItem(DigitizedId);//PropertyTagExifDTDigitized
//2010:12:11 14:15:19
string s = string.Format("{0}:{1}:{2} {3}:{4}:{5}",
creationTime.Year.ToString("0000"),
creationTime.Month.ToString("00"),
creationTime.Day.ToString("00"),
creationTime.Hour.ToString("00"),
creationTime.Minute.ToString("00"),
creationTime.Second.ToString("00"));
byte[] buff = Encoding.UTF8.GetBytes(s);
piDate.Value = new byte[buff.Length + 1];
buff.CopyTo(piDate.Value, 0);
piDate.Value[buff.Length] = 0;
piDate.Len = piDate.Value.Length;
i.SetPropertyItem(piDate);
}
catch
{
}
}