本文整理汇总了C#中System.Drawing.Bitmap.SetResolution方法的典型用法代码示例。如果您正苦于以下问题:C# Bitmap.SetResolution方法的具体用法?C# Bitmap.SetResolution怎么用?C# Bitmap.SetResolution使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Drawing.Bitmap
的用法示例。
在下文中一共展示了Bitmap.SetResolution方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ResizeImageProportionate_XInclination
// This is to resize an image file biased to the width of the image
public void ResizeImageProportionate_XInclination(Stream File_Stream, int Target_Width, string Save_Path)
{
try
{
// This to extract the image from the file stream without uploading
System.Drawing.Image _image = System.Drawing.Image.FromStream(File_Stream);
int Image_Width = _image.Width;
int Image_Height = _image.Height;
int target_width = Target_Width;
int target_height = (Target_Width * Image_Height) / Image_Width;
// This is to create a new image from the file stream to a specified height and width
Bitmap _bitmap = new Bitmap(target_width, target_height, _image.PixelFormat);
_bitmap.SetResolution(72, 72);
// This is to resize the image to the target height and target width
Graphics _graphics = Graphics.FromImage(_bitmap);
_graphics.DrawImage(_image, new Rectangle(0, 0, target_width, target_height),
new Rectangle(0, 0, Image_Width, Image_Height), GraphicsUnit.Pixel);
// This is to save the image file into the save path
_bitmap.Save(Save_Path, _image.RawFormat);
_image.Dispose();
_graphics.Dispose();
_bitmap.Dispose();
}
catch (Exception e)
{
throw e;
}
}
示例2: ResizeImage
// 1) Prevent anti-aliasing.
//...
//g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//// add below line
//g.CompositingMode = CompositingMode.SourceCopy;
//...
//http://stackoverflow.com/questions/4772273/interpolationmode-highqualitybicubic-introducing-artefacts-on-edge-of-resized-im
////resize the image to the specified height and width
//using (var resized = ImageUtilities.ResizeImage(image, 50, 100))
//{
// //save the resized image as a jpeg with a quality of 90
// ImageUtilities.SaveJpeg(@"C:\myimage.jpeg", resized, 90);
//}
// Sourced from:
// http://stackoverflow.com/questions/249587/high-quality-image-scaling-c-sharp
/// <summary>
/// Resize the image to the specified width and height.
/// </summary>
/// <param name="image">The image to resize.</param>
/// <param name="size">The width and height to resize to.</param>
/// <returns>The resized image.</returns>
public static Bitmap ResizeImage(Image image, Size size)
{
if (size.Width < 1 || size.Height < 1)
{
return null;
}
// a holder for the result
var result = new Bitmap(size.Width, size.Height);
//set the resolutions the same to avoid cropping due to resolution differences
result.SetResolution(image.HorizontalResolution, image.VerticalResolution);
//use a graphics object to draw the resized image into the bitmap
using (Graphics graphics = Graphics.FromImage(result))
{
//set the resize quality modes to high quality
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//draw the image into the target bitmap
graphics.DrawImage(image, 0, 0, result.Width, result.Height);
}
//return the resulting bitmap
return result;
}
示例3: Resize
/// <summary>
/// Resizes and rotates an image, keeping the original aspect ratio. Does not dispose the original
/// Image instance.
/// </summary>
/// <param name="image">Image instance</param>
/// <param name="width">desired width</param>
/// <param name="height">desired height</param>
/// <param name="rotateFlipType">desired RotateFlipType</param>
/// <returns>new resized/rotated Image instance</returns>
public static System.Drawing.Image Resize(System.Drawing.Image image, int width,
int height, RotateFlipType rotateFlipType)
{
// clone the Image instance, since we don't want to resize the original Image instance
var rotatedImage = image.Clone() as System.Drawing.Image;
//rotatedImage.RotateFlip(rotateFlipType);
var newSize = CalculateResizedDimensions(rotatedImage, width, height);
var resizedImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format32bppArgb);
resizedImage.SetResolution(72, 72);
using (var graphics = Graphics.FromImage(resizedImage))
{
// set parameters to create a high-quality thumbnail
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
// use an image attribute in order to remove the black/gray border around image after resize
// (most obvious on white images), see this post for more information:
// http://www.codeproject.com/KB/GDI-plus/imgresizoutperfgdiplus.aspx
using (var attribute = new ImageAttributes())
{
attribute.SetWrapMode(WrapMode.TileFlipXY);
// draws the resized image to the bitmap
graphics.DrawImage(rotatedImage, new Rectangle(new Point(0, 0), newSize), 0, 0, rotatedImage.Width, rotatedImage.Height, GraphicsUnit.Pixel, attribute);
}
}
return resizedImage;
}
示例4: ScaleBitmap
private static Bitmap ScaleBitmap(Bitmap image, int width, int height)
{
if (height == -1)
{
var scale = (double)image.Height / (double)image.Width;
height = (int)Math.Ceiling(image.Height * ((double)width / (double)image.Width) * scale);
}
var bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
// set the resolutions the same to avoid cropping due to resolution differences
bmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);
//use a graphics object to draw the resized image into the bitmap
using (Graphics graphics = Graphics.FromImage(bmp))
{
//set the resize quality modes to high quality
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//draw the image into the target bitmap
graphics.DrawImage(image, 0, 0, bmp.Width, bmp.Height);
bmp.MakeTransparent(bmp.GetPixel(0, 0));
}
//return the resulting bitmap
return bmp;
}
示例5: ScaleByPercent
/// <summary>
/// 等比率縮小放大
/// </summary>
/// <param name="imgPhoto">原圖</param>
/// <param name="Percent">比率</param>
/// <returns>放大縮小的結果</returns>
public Image ScaleByPercent(Image imgPhoto, int Percent)
{
float nPercent = ((float)Percent/100);
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX,destY,destWidth,destHeight),
new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
示例6: Resize
/// <summary>
/// Resizes an image
/// </summary>
/// <param name="image">The image to resize</param>
/// <param name="width">New width in pixels</param>
/// <param name="height">New height in pixesl</param>
/// <param name="useHighQuality">Resize quality</param>
/// <returns>The resized image</returns>
public static Bitmap Resize(this Bitmap image, int width, int height, bool useHighQuality)
{
var newImg = new Bitmap(width, height);
newImg.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var g = Graphics.FromImage(newImg))
{
g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
if (useHighQuality)
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
}
else
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.Default;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Default;
}
var attributes = new ImageAttributes();
attributes.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY);
g.DrawImage(image, new System.Drawing.Rectangle(0, 0, width, height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
}
return newImg;
}
示例7: ResizeImage
/// <summary>
/// Resize the image to the specified width and height.
/// </summary>
/// <param name="image">The image to resize.</param>
/// <param name="width">The width to resize to.</param>
/// <param name="height">The height to resize to.</param>
/// <returns>The resized image.</returns>
public static System.Drawing.Image ResizeImage(this System.Drawing.Image image, int width, int height)
{
if ((image.Height < height) && (image.Width < width))
return image;
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
示例8: Crop
public static bool Crop(string _img, string img, int width, int height, int x, int y)
{
try
{
using (Image OriginalImage = Image.FromFile(_img))
{
using (Bitmap bmp = new Bitmap(width, height))
{
bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
using (Graphics Graphic = Graphics.FromImage(bmp))
{
Graphic.SmoothingMode = SmoothingMode.AntiAlias;
Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
Graphic.DrawImage(OriginalImage, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, OriginalImage.RawFormat);
if (File.Exists(img))
File.Delete(img);
File.WriteAllBytes(img, ms.GetBuffer());
return true;
}
}
}
}
catch (Exception Ex)
{
return false;
}
}
示例9: ScaleByPercent
public static Image ScaleByPercent(Image imgPhoto, int Percent)
{
var nPercent = ((float)Percent / 100);
var sourceWidth = imgPhoto.Width;
var sourceHeight = imgPhoto.Height;
var sourceX = 0;
var sourceY = 0;
var destX = 0;
var destY = 0;
var destWidth = (int)(sourceWidth * nPercent + 0.5);
var destHeight = (int)(sourceHeight * nPercent + 0.5);
var bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format32bppArgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
bmPhoto.MakeTransparent();
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.Transparent);
grPhoto.SmoothingMode = SmoothingMode.HighQuality;
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
示例10: DrawBitmap
/// <summary>
/// The G910 also updates the G-logo, G-badge and G-keys
/// </summary>
/// <param name="bitmap"></param>
public override void DrawBitmap(Bitmap bitmap)
{
using (var croppedBitmap = new Bitmap(21*4, 6*4))
{
// Deal with non-standard DPI
croppedBitmap.SetResolution(96, 96);
// Don't forget that the image is upscaled 4 times
using (var g = Graphics.FromImage(croppedBitmap))
{
g.DrawImage(bitmap, new Rectangle(0, 0, 84, 24), new Rectangle(4, 4, 84, 24), GraphicsUnit.Pixel);
}
base.DrawBitmap(croppedBitmap);
}
using (var resized = OrionUtilities.ResizeImage(bitmap, 22, 7))
{
// Color the extra keys on the left side of the keyboard
SetLogitechColorFromCoordinates(resized, KeyboardNames.G_LOGO, 0, 1);
SetLogitechColorFromCoordinates(resized, KeyboardNames.G_1, 0, 2);
SetLogitechColorFromCoordinates(resized, KeyboardNames.G_2, 0, 3);
SetLogitechColorFromCoordinates(resized, KeyboardNames.G_3, 0, 4);
SetLogitechColorFromCoordinates(resized, KeyboardNames.G_4, 0, 5);
SetLogitechColorFromCoordinates(resized, KeyboardNames.G_5, 0, 6);
// Color the extra keys on the top of the keyboard
SetLogitechColorFromCoordinates(resized, KeyboardNames.G_6, 3, 0);
SetLogitechColorFromCoordinates(resized, KeyboardNames.G_7, 4, 0);
SetLogitechColorFromCoordinates(resized, KeyboardNames.G_8, 5, 0);
SetLogitechColorFromCoordinates(resized, KeyboardNames.G_9, 6, 0);
// Color the G-badge
SetLogitechColorFromCoordinates(resized, KeyboardNames.G_BADGE, 5, 6);
}
}
示例11: Post
// POST api/imagerecognition
public double[] Post([FromBody]object text)
{
var txt = System.Web.HttpContext.Current.Request.Form["text"];
txt = txt.Substring("data:image/png;base64,".Count());
var imgData = Convert.FromBase64String(txt);
var path = System.Web.Hosting.HostingEnvironment.MapPath("~/") + "\\tempimg.png";
var path1 = System.Web.Hosting.HostingEnvironment.MapPath("~/") + "\\tempimg.bmp";
using (var img = System.IO.File.OpenWrite(path))
{
img.Write(imgData, 0, imgData.Count());
img.Close();
img.Dispose();
}
Image img1 = Image.FromFile(path);
using (Bitmap b = new Bitmap(img1.Width, img1.Height))
{
b.SetResolution(img1.HorizontalResolution, img1.VerticalResolution);
using (Graphics g = Graphics.FromImage(b))
{
g.Clear(Color.White);
g.DrawImageUnscaled(img1, 0, 0);
}
b.Save(path1,System.Drawing.Imaging.ImageFormat.Bmp);
// Now save b as a JPEG like you normally would
}
img1.Dispose();
return ImageRecognitionContext.OCR.RecognizeImage(Image.FromFile(path1));
}
示例12: ToFixedSize
private static Image ToFixedSize(this Image imgPhoto, int width, int height)
{
var dimensions = ResizeKeepAspect(imgPhoto.Size, width, height);
var bmPhoto = new Bitmap(width, height,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);
using (var grPhoto = Graphics.FromImage(bmPhoto))
{
grPhoto.Clear(Color.White);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new RectangleF(dimensions.X, dimensions.Y, dimensions.Width, dimensions.Height),
new RectangleF(0, 0, imgPhoto.Width, imgPhoto.Height),
GraphicsUnit.Pixel);
}
imgPhoto.Dispose();
return bmPhoto;
}
示例13: CropCommandClick
protected void CropCommandClick(object sender, EventArgs e)
{
var x = int.Parse(_xField.Value);
var y = int.Parse(_yField.Value);
var width = int.Parse(_widthField.Value);
var height = int.Parse(_heightField.Value);
using (var photo = Image.FromFile(Server.MapPath("~/Images/akropolis-doggie.jpg")))
using (var result = new Bitmap(width, height, photo.PixelFormat))
{
result.SetResolution(
photo.HorizontalResolution,
photo.VerticalResolution);
using (var g = Graphics.FromImage(result))
{
g.InterpolationMode =
InterpolationMode.HighQualityBicubic;
g.DrawImage(photo,
new Rectangle(0, 0, width, height),
new Rectangle(x, y, width, height),
GraphicsUnit.Pixel);
photo.Dispose();
result.Save(Server.MapPath("~/Images/cropped_doggie.jpg"));
}
}
}
示例14: ResizeImage
//Метод для высококачественного изменения размера изображения
public static Image ResizeImage(Image image, int width, int height)
{
//Если изменять нечего, возвращаем исходное изображение без изменений
if ((image.Width == width && image.Height == height) || (width == 0 && height == 0))
return new Bitmap(image);
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
示例15: GetBitmap
public Bitmap GetBitmap(Size size, int dpi)
{
Contract.Ensures(Contract.Result<Bitmap>() != null);
Contract.Ensures((Contract.Result<Bitmap>().PixelFormat & PixelFormat.Indexed) == 0);
Bitmap bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppRgb);
bitmap.SetResolution(dpi, dpi);
Contract.Assume((bitmap.PixelFormat & PixelFormat.Indexed) == 0);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
using (Pen pen = new Pen(Brushes.Black, (float)Math.Ceiling(dpi / 150d)))
{
foreach (Triangle triangle in this.Triangles)
{
triangle.DrawTriangle(graphics, size, 10000f * (float)Math.Ceiling(dpi / 300d), pen);
}
}
Rectangle rect = new Rectangle(0, 0, size.Width, size.Height);
LinearGradientBrush linearGradientBrush = new LinearGradientBrush(
rect,
Color.FromArgb(64, Color.Black),
Color.FromArgb(0, Color.Black),
LinearGradientMode.Vertical);
graphics.FillRectangle(linearGradientBrush, rect);
}
Contract.Assume((bitmap.PixelFormat & PixelFormat.Indexed) == 0);
return bitmap;
}