.NET - 边框调整大小成像 [英] .NET - Border around resized imaged

查看:211
本文介绍了.NET - 边框调整大小成像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想调整在.NET中的图像,但避开了调整后的图像淡淡的黑色边框。我发现了一个帖子 - <一个href="http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/cf765094-c8c1-4991-a1f3-cecdbd07ee15/" rel="nofollow">http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/cf765094-c8c1-4991-a1f3-cecdbd07ee15/它从别人谁制作所述目标矩形大于画布的工作,但是这并没有为我工作。它得到RIID顶部和左侧边界,但右侧和底部仍然存在,并且是一个完整的1px的浓黑的。

我缺少的东西?我的code是如下。

 图片图像= ... //这是从源头上装载的有效图像
矩形srcRectangle =新的Rectangle(0,0,宽,高);
        大小croppedFullSize =新尺寸(宽+ 3,身高+ 3);
        矩形destRect =新的矩形(新点(-1,-1),croppedFullSize);
        使用(位图newImage =新位图(croppedFullSize.Width,croppedFullSize.Height,格式))
        使用(图形画布= Graphics.FromImage(newImage)){
            Canvas.Smoothi​​ngMode = Smoothi​​ngMode.AntiAlias​​;
            Canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
            Canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
            Canvas.FillRectangle(Brushes.Transparent,destRect);
            Canvas.DrawImage(图像,destRect,srcRectangle,GraphicsUnit.Pixel);


            newImage.Save(文件名,image.RawFormat);
        }
 

解决方案

试试这样说,我想我从来没有黑边......

如果你想使用System.Drawing中的库:

 使用(VAR sourceBmp =新位图(SOURCEPATH))
{
  十进制方面=(十进制)sourceBmp.Width /(十进制)sourceBmp.Height;
  INT newHeight =(INT)(newWidth /面);

   使用(VAR destinationBmp =新位图(newWidth,newHeight))
   {
     使用(VAR destinationGfx = Graphics.FromImage(destinationBmp))
     {
       destinationGfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
       destinationGfx.DrawImage(sourceBmp,新矩形(0,0,destinationBmp.Width,destinationBmp.Height));
       destinationBmp.Save(的DestinationPath,ImageFormat.Jpeg);
      }
    }
}
 

或者你可以用WPF做同样的,像这样的:

 使用(VAR输出=新的FileStream(outputPath,FileMode.CreateNew,FileAccess.ReadWrite,FileShare.None))
{
   VAR imageDe codeR = BitmapDe coder.Create(InputStream中,BitmapCreateOptions preservePixelFormat,BitmapCacheOption.None);
   VAR IMAGEFRAME = imageDe coder.Frames [0];

   十进制方面=(十进制)imageFrame.Width /(十进制)imageFrame.Height;
   VAR HEIGHT =(INT)(newWidth /面);

   VAR imageResized =新TransformedBitmap(IMAGEFRAME,新ScaleTransform(
                                                                 newWidth / imageFrame.Width * DPI / imageFrame.DpiX,
                                                                 高度/ imageFrame.Height * DPI / imageFrame.DpiY,0,0));

   VAR targetFrame = BitmapFrame.Create(imageResized);

   VAR targetEn codeR =新JpegBitmapEn codeR();
   targetEn coder.Frames.Add(targetFrame);
   targetEn coder.QualityLevel = 80;
   targetEn coder.Save(输出);
}
 

我推荐WPF的方式。在COM pression和放大器;质量似乎更好...

I'm trying to resize an image in .NET, but get a faint black border around the resized image. I found a post - http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/cf765094-c8c1-4991-a1f3-cecdbd07ee15/ which from someone who said making the destination rectangle larger than the canvas worked, but this doesn't work for me. It gets riid of the top and left borders, but the right and bottom are still there, and are a full 1px thick black.

Am I missing something? My code is below.

Image image = ... // this is a valid image loaded from the source
Rectangle srcRectangle = new Rectangle(0,0,width, height);
        Size croppedFullSize = new Size(width+3,height+3);
        Rectangle destRect = new Rectangle(new Point(-1,-1), croppedFullSize);
        using(Bitmap newImage = new Bitmap(croppedFullSize.Width, croppedFullSize.Height, format))
        using(Graphics Canvas = Graphics.FromImage(newImage)) {
            Canvas.SmoothingMode = SmoothingMode.AntiAlias;
            Canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
            Canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
            Canvas.FillRectangle(Brushes.Transparent, destRect);
            Canvas.DrawImage(image, destRect, srcRectangle, GraphicsUnit.Pixel);


            newImage.Save(filename, image.RawFormat);
        }

解决方案

Try it like this, i think i've never got a black border...

If you want to use System.Drawing libraries:

using (var sourceBmp = new Bitmap(sourcePath))
{
  decimal aspect = (decimal)sourceBmp.Width / (decimal)sourceBmp.Height;
  int newHeight = (int)(newWidth / aspect);

   using (var destinationBmp = new Bitmap(newWidth, newHeight))
   {
     using (var destinationGfx = Graphics.FromImage(destinationBmp))
     {
       destinationGfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
       destinationGfx.DrawImage(sourceBmp, new Rectangle(0, 0, destinationBmp.Width, destinationBmp.Height));
       destinationBmp.Save(destinationPath, ImageFormat.Jpeg);
      }
    }
}

or you can do the same with wpf, like this:

using (var output = new FileStream(outputPath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None))
{
   var imageDecoder = BitmapDecoder.Create(inputStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
   var imageFrame = imageDecoder.Frames[0];

   decimal aspect = (decimal)imageFrame.Width / (decimal)imageFrame.Height;
   var height = (int)(newWidth / aspect);

   var imageResized = new TransformedBitmap(imageFrame,new ScaleTransform(
                                                                 newWidth / imageFrame.Width * Dpi / imageFrame.DpiX,
                                                                 height / imageFrame.Height * Dpi / imageFrame.DpiY, 0, 0));

   var targetFrame = BitmapFrame.Create(imageResized);

   var targetEncoder = new JpegBitmapEncoder();
   targetEncoder.Frames.Add(targetFrame);
   targetEncoder.QualityLevel = 80;
   targetEncoder.Save(output);
}

I recommend the WPF way. The compression & quality seems better...

这篇关于.NET - 边框调整大小成像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆