Graphics.DrawImage变暗了?
我使用 DrawImage Image 函数将图像的一部分复制到另一个图像。 是否可以使复制的部分“更暗”?比如,特殊的色调?有人能给我举个例子吗?我听说过一个叫做 ImageAttributes 的东西,但我找不到它!
注意:我不想编辑源图像。
I use the DrawImage Image function to copy a portion from an image to another.
Is it possible to make it so the copied portion is "darker"? Like, a special color tone? Can someone give me an example? I heard of something called ImageAttributes but I can't find it!
Note: I don't want to edit the source image.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
绘制图形很慢,尤其是当您同时应用渲染效果时。花几分钟在 Photoshop 中工作,您就会明白我的意思。使图像变暗是一项计算成本高昂的任务。您几乎无能为力,可以让它运行得更快。
.NET Framework 中的 System.Drawing.Graphics 类中的例程是使用 GDI+ 在内部实现的,而 GDI+ 不是硬件加速的,这一事实使问题变得更加复杂。一种可能的替代方案是切换回基于 GDI 的渲染,它在大多数系统上都是硬件加速的(这取决于您的显卡供应商,但您今天找到的几乎每张显卡都具有用于位块传输等基本功能的硬件加速) 。当然,这要困难得多,因为您需要从 Windows API 中 P/Invoke 函数才能使用基于 GDI 的绘图例程; .NET 为您提供的只是 GDI+。这是比必要的工作多得多的工作,并且在现代硬件上速度的改进可能很小(特别是启用了 Windows Vista/7 的 Aero 主题,即使使用 GDI,它也不使用硬件加速,因为所有内容都绘制到内存中的位图) 。
坚持当前的实现,一些可能的优化仍然浮现在脑海中:
重画图像时是否调整图像大小?如果是这样,则需要对图像进行插值,相比之下,这非常慢。
图像采用什么
PixelFormat
?Format32bppPArgb
比替代方案快得多。确保您正在使用该格式的图像。如果您现在还没有这样做,那么进行此更改应该会看到渲染速度大幅提高。为什么需要如此频繁地重新渲染图像?如果是相同的图像,则在应用效果后绘制它并缓存返回的
Bitmap
对象。然后,每次需要重绘图像时,请使用内存中已有的图像,而不是创建新图像。这是一个简单的技巧,但它可以让您不必每次都重复执行昂贵的部分。正如您提到的,单独使用DrawImage
比设置渲染属性要快得多。一般来说,用户愿意等待大图像渲染。在这里,相应地设置界面比尝试优化图形例程更合适。对于用户而言,使用进度条和繁忙的光标来指示实际正在发生的事情比在代码中多压缩几毫秒要好得多。除非您在循环中反复应用此效果大约 100 或 1000 次(这看起来很愚蠢),否则不值得花费时间进行优化。
Drawing graphics is slow, especially when you're applying a rendering effect at the same time. Spend a few minutes working in Photoshop, and you'll see what I mean. Darkening the image is a computationally expensive task. There's little you can do to make it go faster.
The problem is further compounded by the fact that the routines in the
System.Drawing.Graphics
class in the .NET Framework are internally implemented using GDI+, which is not hardware accelerated. A possible alternative is to switch back to GDI-based rendering, which is hardware accelerated on most systems (it depends on your video card's vendor, but almost every card you'll find today has hardware acceleration for basic functions like bit-block transfers). Of course, this is a lot more difficult because you'll need to P/Invoke functions from the Windows API to use GDI-based drawing routines; all that .NET provides for you is GDI+. It's a whole lot more work than necessary, and the speed improvements are probably minimal on modern hardware (especially with Windows Vista/7's Aero theme enabled, which doesn't use hardware acceleration even with GDI because everything is drawn to a bitmap in memory).Sticking with the current implementation, a couple of possible optimizations do yet spring to mind:
Are you resizing the image when you redraw it? If so, that requires the image to be interpolated, which is extremely slow by comparison.
What
PixelFormat
is the image in?Format32bppPArgb
is much faster than the alternatives. Make sure that you're working with an image in that format. If you're not now, making this change should see your rendering speed increase substantially.Why do you need to re-render the image so often? If it's the same image, draw it once applying the effects and cache the
Bitmap
object that is returned. Then each time you need to redraw the image, use the one already in memory rather than creating a new one. It's a simple trick, but it keeps you from having to do the expensive part again each time. As you mention,DrawImage
alone is much faster than setting rendered attributes.Generally, users are willing to wait for large images to render. Setting up your interface accordingly is more appropriate here than trying to optimize the graphics routines. That a progress bar and a busy cursor are used to indicate something is actually happening will go a lot further as far as the user is concerned than squeezing a few more milliseconds out of your code. Unless you're applying this effect over and over in a loop some 100 or 1000 times (which seems pretty silly), it isn't worth the time it would take to optimize.
DrawImage 函数甚至有一个特殊的重载,可以准确地设置 ImageAttributes 对象:
The DrawImage function even has a special overload that sets an ImageAttributes object exactly for that reason: