这里简单的好奇心,带着一些实际问题,因为我偶尔会被这个问题所困扰.
我正在尝试调整图像大小,同时保留原始图像的宽高比,这样新图像看起来不会被压扁.
例如:
将150*100图像转换为150*150图像.
高度的额外50个像素需要用白色背景颜色填充.
这是我正在使用的当前代码.
它适用于调整大小,但更改原始图像的宽高比会压缩新图像.
private void resizeImage(string path, string originalFilename,
int width, int height)
{
Image image = Image.FromFile(path + originalFilename);
System.Drawing.Image thumbnail = new Bitmap(width, height);
System.Drawing.Graphics graphic =
System.Drawing.Graphics.FromImage(thumbnail);
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphic.CompositingQuality = CompositingQuality.HighQuality;
graphic.DrawImage(image, 0, 0, width, height);
System.Drawing.Imaging.ImageCodecInfo[] info =
ImageCodecInfo.GetImageEncoders();
EncoderParameters encoderParameters;
encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality,
100L);
thumbnail.Save(path + width + "." + originalFilename, info[1],
encoderParameters);
}
Run Code Online (Sandbox Code Playgroud)
编辑:我想填充图像而不是裁剪
我已将我的项目从.NET 4.5更新到.NET Core(使用ASP.NET Core).我在之前的版本中有一些非常简单的代码,它使用位图对象System.Drawing来调整图像大小.
据我所知System.Drawing,不能在.NET Core中使用,因为它不是跨平台的,但可以使用什么呢?
我用Google搜索了一下,找不到任何东西.我唯一能找到的就是这篇文章,它上面没有任何代码.
我正在努力上传并将该图像的缩略图副本保存在缩略图文件夹中.
我正在使用以下链接:
http://weblogs.asp.net/markmcdonnell/archive/2008/03/09/resize-image-before-uploading-to-server.aspx
但
newBMP.Save(directory + "tn_" + filename);
Run Code Online (Sandbox Code Playgroud)
导致异常"GDI +中发生了一般错误."
我试图给予文件夹权限,还试图在保存时使用新的单独的bmp对象.
编辑:
protected void ResizeAndSave(PropBannerImage objPropBannerImage)
{
// Create a bitmap of the content of the fileUpload control in memory
Bitmap originalBMP = new Bitmap(fuImage.FileContent);
// Calculate the new image dimensions
int origWidth = originalBMP.Width;
int origHeight = originalBMP.Height;
int sngRatio = origWidth / origHeight;
int thumbWidth = 100;
int thumbHeight = thumbWidth / sngRatio;
int bannerWidth = 100;
int bannerHeight = bannerWidth / sngRatio;
// Create a new …Run Code Online (Sandbox Code Playgroud) 我从另一个应用程序获取图标使用此:
Icon IEIcon = Icon.ExtractAssociatedIcon(@"C:\Program Files\Internet Explorer\iexplore.exe");
Run Code Online (Sandbox Code Playgroud)
如何将其转换为System.Drawing.Image?
提前致谢.
经过几天跟踪奇怪的GDI +错误,我偶然发现了MSDN上的这个小宝石:
System.Drawing命名空间中的类不支持在Windows或ASP.NET服务中使用.尝试在其中一种应用程序类型中使用这些类可能会产生意外问题,例如服务性能下降和运行时异常.
我不知道"ASP.NET服务"在这种情况下是否意味着"Web应用程序",但"服务性能降低"当然似乎涵盖了"GDI +中出现一般性错误"和"内存不足"错误的随机分类我的应用程序正在抛出 - 读取和写入JPEG图像的间歇性,不可重现的错误 - 在许多情况下 - 实际上是由System.Drawing.Imaging首先创建的.
那么 - 如果GDI +无法在Web应用程序中可靠地读取和写入JPEG文件,那么我应该使用什么呢?
我希望用户能够上传图像(需要JPEG,其他格式很好),可靠地重新采样,并在出现任何问题时显示有用的错误消息.有任何想法吗?WPF的System.Media命名空间值得考虑吗?
编辑:是的,我知道GDI +"大部分时间都在工作".这还不够好,因为当它失败时,它会以一种无法隔离或优雅地恢复的方式这样做.我对适合您的GDI +代码示例不感兴趣:我正在寻找用于图像处理的替代库.
将文本渲染到位图时,我发现当在具有非不透明alpha的区域上渲染时,文本看起来非常糟糕.随着基础像素变得更加透明,问题逐渐恶化.如果我不得不猜测我会说当底层像素是透明的时,文本渲染器将任何消除锯齿的"灰色"像素绘制为纯黑色.
以下是一些截图:
在透明像素上绘制的文字:

在半透明像素上绘制的文字:

在不透明像素上绘制的文字:

以下是用于呈现文本的代码:
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawString("Press the spacebar", Font, Brushes.Black, textLeft, textTop);
Run Code Online (Sandbox Code Playgroud) 拥有一个在加载和存储图像时可以使用多年的代码,我发现我有一个图像打破了这段代码:
const string i1Path = @"c:\my\i1.jpg";
const string i2Path = @"c:\my\i2.jpg";
var i = Image.FromFile(i1Path);
i.Save(i2Path, ImageFormat.Jpeg);
Run Code Online (Sandbox Code Playgroud)
例外是:
发生了System.Runtime.InteropServices.ExternalException
GDI +中发生了一般错误.
at System.Drawing.Image.Save(String filename,ImageCodecInfo encoder,EncoderParameters encoderParams)
at System.Drawing.Image.Save(String filename,ImageFormat format)
at ...
据我所知,图像没什么特别之处.它的大小约为250像素,可以在Windows Image Viewer或Paint.NET中打开:

(由于上面的图片,上传到Stack Overflow后不再产生错误,我把原始图像放在这里)
我发现在调用Save方法时,目标图像文件是用零字节创建的.
我真的对导致错误的原因一无所知.
我的问题:
我正在为我们公司开发WinForm Printing应用程序.
打印文档时,我需要获取System.Drawing.Color文档上每个Control 的属性并创建一个System.Drawing.Brush对象来绘制它.
有没有办法将System.Drawing.Color值转换为System.Drawing.Brush值?
注意:我已经尝试过查看该System.Windows.Media.SolidColorBrush()方法,但似乎没有帮助.
c# system.drawing printdocument system.drawing.color winforms
我使用Bing地图WPF控件创建了一个WPF应用程序.我希望能够只截取Bing地图控件.
使用此代码制作屏幕截图:
// Store the size of the map control
int Width = (int)MyMap.RenderSize.Width;
int Height = (int)MyMap.RenderSize.Height;
System.Windows.Point relativePoint = MyMap.TransformToAncestor(Application.Current.MainWindow).Transform(new System.Windows.Point(0, 0));
int X = (int)relativePoint.X;
int Y = (int)relativePoint.Y;
Bitmap Screenshot = new Bitmap(Width, Height);
Graphics G = Graphics.FromImage(Screenshot);
// snip wanted area
G.CopyFromScreen(X, Y, 0, 0, new System.Drawing.Size(Width, Height), CopyPixelOperation.SourceCopy);
string fileName = "C:\\myCapture.bmp";
System.IO.FileStream fs = System.IO.File.Open(fileName, System.IO.FileMode.OpenOrCreate);
Screenshot.Save(fs, System.Drawing.Imaging.ImageFormat.Bmp);
fs.Close();
Run Code Online (Sandbox Code Playgroud)
我的问题:
在Width和Height似乎是坏的(假值).生成的屏幕截图似乎使用了不良坐标.
我的截图:

我期待的是:

为什么我得到这个结果?我尝试在发布模式下,没有Visual Studio,结果是一样的.
system.drawing ×10
c# ×9
.net ×4
gdi+ ×4
image ×2
winforms ×2
.net-core ×1
asp.net ×1
asp.net-core ×1
drawstring ×1
icons ×1
screenshot ×1
wpf ×1