本文整理汇总了C#中System.Drawing.Image.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# Image.Dispose方法的具体用法?C# Image.Dispose怎么用?C# Image.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Drawing.Image
的用法示例。
在下文中一共展示了Image.Dispose方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Add
public void Add(Image img)
{
_list.Add(GetBytes(img));
// stored image data, now dispose of original
img.Dispose();
}
示例2: DetectAndTrimFace
public static Image<Gray, byte> DetectAndTrimFace(int[] pixels, Size initialSize, Size outputSize, String haarcascadePath)
{
var inBitmap = ConvertToBitmap(pixels, initialSize.Width, initialSize.Width);
//for testing purposes I can the picture to a folder
//inBitmap.Save(@"E:\data\phototest\received.bmp");
var grayframe = new Image<Gray, byte>(inBitmap);
var haar = new HaarCascade(haarcascadePath);
var faces = haar.Detect(grayframe,
1.2,
3,
HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
new Size(30, 30));
if (faces.Count() != 1)
{
return null;
}
var face = faces[0];
var returnImage = grayframe.Copy(face.rect).Resize(outputSize.Width, outputSize.Height, INTER.CV_INTER_CUBIC);
//cleanup managed resources
haar.Dispose();
grayframe.Dispose();
return returnImage;
}
示例3: Crop
public static Image Crop(Image originalImage, RectangleF rect)
{
Bitmap bmpImage = new Bitmap(originalImage);
Bitmap bmpCrop = bmpImage.Clone(rect, bmpImage.PixelFormat);
originalImage.Dispose();
return (Image)(bmpCrop);
}
示例4: ResizeImage
/// <summary>
/// resmi yeniden boyutlandır.
/// </summary>
/// <param name="imgToResize">boyutlandırılacak resim</param>
/// <param name="size">boyutlar</param>
/// <returns>Image titipnde bir resim</returns>
public static Image ResizeImage(Image imgToResize, Size size)
{
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)size.Width / (float)sourceWidth);
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
imgToResize.Dispose();
return (Image)b;
}
示例5: ResizeImage
public static Image ResizeImage(double scaleFactor, Image image)
{
MemoryStream stream = new MemoryStream();
var newWidth = (int)(image.Width * scaleFactor);
var newHeight = (int)(image.Height * scaleFactor);
var thumbnailBitmap = new Bitmap(newWidth, newHeight);
var thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
thumbnailGraph.DrawImage(image, imageRectangle);
thumbnailBitmap.Save(stream, image.RawFormat);
Image returnImage = Image.FromStream(stream);
thumbnailGraph.Dispose();
thumbnailBitmap.Dispose();
image.Dispose();
return returnImage;
}
示例6: TakeScreenshot
/// <summary>
/// Most of the time it just returns a black screen, this is because the game is rendering straight to the
/// graphics card using directx and bypassing whatever windows usually uses for PrintWindow
/// AFAIK directx screenshots only work if the window is currently visible (not overlaid by the bot itself)
/// </summary>
/// <returns></returns>
public Image<Bgr, byte> TakeScreenshot()
{
Bitmap frame = new Bitmap(1024, 768);
using (Graphics g = Graphics.FromImage(frame))
{
IntPtr deviceContextHandle = g.GetHdc();
PrintWindow(GetGameWindowHandle(), deviceContextHandle, 1);
g.ReleaseHdc();
}
//we have the bitmap now
//turn it into an Image for emgu
BitmapData bmpData = frame.LockBits(new Rectangle(0, 0, frame.Width, frame.Height), ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);
Image<Bgr, byte> tempImage = new Image<Bgr, byte>(frame.Width, frame.Height, bmpData.Stride, bmpData.Scan0);
//to prevent any corrupted memory errors that crop up for some reason
Image<Bgr, byte> image = tempImage.Clone();
frame.UnlockBits(bmpData);
//dispose all unused image data to prevent memory leaks
frame.Dispose();
tempImage.Dispose();
image.Save("screenshot.png");
return image;
}
示例7: 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;
}
示例8: Process
public Image Process(Image inputImage, out bool isProcessed)
{
if (this.TargetSize.Height > inputImage.Height && this.TargetSize.Width > inputImage.Width)
{
isProcessed = false;
return inputImage;
}
Size arg_39_0 = inputImage.Size;
Rectangle cropArea = this.CropArea;
int num = cropArea.X + cropArea.Width;
if (num > inputImage.Width)
{
cropArea.Width -= num - inputImage.Width;
}
int num2 = cropArea.Y + cropArea.Height;
if (num2 > inputImage.Height)
{
cropArea.Height -= num2 - inputImage.Height;
}
Bitmap bitmap = new Bitmap(this.TargetSize.Width, this.TargetSize.Height);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.InterpolationMode = this.InterpoliationMode;
graphics.SmoothingMode = this.SmoothingMode;
Rectangle destRect = new Rectangle(0, 0, this.TargetSize.Width, this.TargetSize.Height);
graphics.DrawImage(inputImage, destRect, cropArea, GraphicsUnit.Pixel);
}
inputImage.Dispose();
isProcessed = true;
return bitmap;
}
示例9: CheckValidImage
public Image CheckValidImage(Image img, int width, int height)
{
if (img == null) return null;
Bitmap bmp = new Bitmap(width, height);
try
{
using (Graphics g = Graphics.FromImage(bmp))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawImage(img, 0, 0, width, height);
}
bmp.Tag = img.Tag;
return bmp;
}
catch (Exception)
{
bmp.Dispose();
bmp = new Bitmap(width, height);
bmp.Tag = img.Tag;
return bmp;
}
finally
{
img.Dispose();
}
}
示例10: Main
static void Main(string[] args)
{
var img = new Image<Bgr, byte>(@"src.jpg");
var w = img.Width;
var h = img.Height;
var r0 = 512; // half of the image size
var p = 0.5; // control the "height"
var phi0 = -Math.PI / 2 + 0.12; // initial phase
var newImg = new Image<Bgr, byte>(r0 * 2, r0 * 2, new Bgr(Color.White));
for (int row = 0; row < r0 * 2; row++)
{
for (int col = 0; col < r0 * 2; col++)
{
var phi = (Math.Atan2(row - r0, col - r0) + phi0);
if (phi > Math.PI) phi -= 2 * Math.PI;
if (phi < -Math.PI) phi += 2 * Math.PI;
var r = Math.Sqrt((row - r0) * (row - r0) + (col - r0) * (col - r0));
r = Math.Pow(r0, 1 - p) * Math.Pow(r, p);
var y = (int)(h - r / r0 * h);
var x = (int)((phi + Math.PI) / Math.PI / 2 * w);
if (y >= 0 && y < h && x >= 0 && x < w)
newImg[row, col] = img[y, x];
}
}
newImg.Save("result.jpg");
newImg.Dispose();
img.Dispose();
}
示例11: PagingImage
public PagingImage(string filename)
{
_filename = filename;
img = Bitmap.FromFile(filename);
_pages = img.GetFrameCount(FrameDimension.Page);
img.Dispose();
}
示例12: Crop
public static Image Crop(Image Image, int newWidth, int newHeight, int startX = 0, int startY = 0)
{
if (Image.Height < newHeight)
newHeight = Image.Height;
if (Image.Width < newWidth)
newWidth = Image.Width;
using (var bmp = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb))
{
bmp.SetResolution(72, 72);
using (var g = Graphics.FromImage(bmp))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawImage(Image, new Rectangle(0, 0, newWidth, newHeight), startX, startY, newWidth, newHeight, GraphicsUnit.Pixel);
var ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Png);
Image.Dispose();
var outimage = Image.FromStream(ms);
return outimage;
}
}
}
示例13: ParseImage
public int[][] ParseImage(Image pic)
{
Bitmap bitmapPic = new Bitmap(this.MATRIX_WIDTH, this.MATRIX_HEIGHT);
// cut smallest square around the letter
Rectangle letterSquare = this.TrimLetter(new Bitmap(pic));
// resize it
Graphics graphics = Graphics.FromImage(bitmapPic);
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.DrawImage(pic,
new Rectangle(0, 0, this.MATRIX_WIDTH, this.MATRIX_HEIGHT),
letterSquare,
GraphicsUnit.Pixel);
// convert data to int[][]
int[][] resultMatrix = this.DataToString(ref bitmapPic);
graphics.Dispose();
bitmapPic.Dispose();
pic.Dispose();
return resultMatrix;
}
示例14: ResizeImage
public static Image ResizeImage (Image img, string path, Size maxSize) {
// Mono for mac uses a better system.drawing implementation; fall back to graphicsmagick on linux
if (Extensions.IsRunningOnMac) {
var maxAspect = (float)maxSize.Width / (float)maxSize.Height;
var imgAspect = (float)img.Width / (float)img.Height;
if (imgAspect > maxAspect)
return new Bitmap(img, new Size(maxSize.Width, (int)Math.Round(maxSize.Width / imgAspect)));
else
return new Bitmap(img, new Size((int)Math.Round(maxSize.Height * imgAspect), maxSize.Height));
} else {
img.Dispose();
var wand = GraphicsMagick.NewWand();
GraphicsMagick.ReadImageBlob(wand, File.OpenRead(path));
var maxAspect = (float)maxSize.Width / (float)maxSize.Height;
var imgAspect = (float)GraphicsMagick.GetWidth(wand) / (float)GraphicsMagick.GetHeight(wand);
if (imgAspect > maxAspect)
GraphicsMagick.ResizeImage(wand, (IntPtr)maxSize.Width, (IntPtr)Math.Round(maxSize.Width / imgAspect), GraphicsMagick.Filter.Box, 1);
else
GraphicsMagick.ResizeImage(wand, (IntPtr)Math.Round(maxSize.Height * imgAspect), (IntPtr)maxSize.Height, GraphicsMagick.Filter.Box, 1);
var newImgBlob = GraphicsMagick.WriteImageBlob(wand);
using (var ms = new MemoryStream(newImgBlob)) {
return Image.FromStream(ms);
}
}
}
示例15: MakeSquareImage
public static void MakeSquareImage(Image image, string newFileName, int newSize)
{
int width = image.Width;
int height = image.Height;
var b = new Bitmap(newSize, newSize);
try
{
Graphics g = Graphics.FromImage(b);
g.InterpolationMode = InterpolationMode.High;
g.SmoothingMode = SmoothingMode.HighQuality;
g.Clear(Color.Transparent);
g.DrawImage(image, new Rectangle(0, 0, newSize, newSize),
width < height
? new Rectangle(0, (height - width)/2, width, width)
: new Rectangle((width - height)/2, 0, height, height), GraphicsUnit.Pixel);
SaveImage(b, newFileName, GetCodecInfo("image/" + GetFormat(newFileName).ToString().ToLower()));
}
finally
{
image.Dispose();
b.Dispose();
}
}