- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我使用 Graphics.DrawImage
绘制图像并以比原始图像更大的尺寸绘制时,它最终变得有点太小了。您可以在下图中看到这一点:
绿线不应该是可见的,也不是图像的一部分。相反,它们被绘制在图像后面,图像应该覆盖它们。
如何绘制尺寸完全正确的图像?
编辑:我使用传递给 DrawImage
调用的相同矩形绘制绿色部分,并使用图像应该有多大的确切尺寸。所以我的值(value)观没有缺陷(我认为)。
编辑 2:我使用 FillRectangle
绘制绿色矩形,因此不需要进行笔计算。此外,我记录了传递到矩形中的图像和绿色填充值,这些值是正确的。这只是关闭的图像。我稍后会发布代码,因为我现在不在电脑前。
编辑 3:这是我用来渲染图像的代码:
// This is for zooming
public readonly float[] SCALES = { 0.05f, 0.1f, 0.125f, 0.25f, 0.333f, 0.5f, 0.667f, 0.75f, 1.0f, 1.25f, 1.5f, 1.75f, 2.0f, 2.5f, 3.0f, 3.5f, 4.0f, 4.5f, 5.0f, 6.0f, 7.0f, 8.0f, 10.0f, 12.0f, 15.0f, 20.0f, 30.0f, 36.0f };
private int scaleIndex = 8;
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
float ScaleFactor = SCALES[scaleIndex];
e.Graphics.InterpolationMode = ScaleFactor < 1 ? InterpolationMode.Bicubic : InterpolationMode.NearestNeighbor;
Image im = Properties.Resources.TSprite0;
for (int y = 0; y < TilesVertical; y++)
{
for (int x = 0; x < TilesHorizontal; x++)
{
float sx = im.Width * ScaleFactor;
float sy = im.Height * ScaleFactor;
Point p = new Point((int)(-scrollPosition.X + sx * x), (int)(-scrollPosition.Y + sy * y));
Size s = new Size((int)Math.Floor(sx), (int)Math.Floor(sy));
// The green rectangle in the background should be the same size as the image
e.Graphics.FillRectangle(Brushes.Lime, new Rectangle(p, s));
e.Graphics.DrawImage(im, new Rectangle(p, s), 0, 0, 16, 16, GraphicsUnit.Pixel);
}
}
im.Dispose();
}
编辑 4:另请注意,图像似乎在左侧和顶部被裁剪,而不是调整大小。看一下在 Photoshop 中放大的原始图像与 GDI+ 如何渲染它的比较:
最佳答案
当缩放到 2 倍或更大时会出现此问题。
看起来整个问题都是由错误的默认值引起的PixelOffsetMode .
By offsetting pixels during rendering, you can improve render quality at the cost of render speed.
将其设置为
g.PixelOffsetMode = PixelOffsetMode.Half;
让它为我消失。
将其设置为
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
也可以正常工作。
Default
、None
和 HighSpeed
会导致图像向左上方稍微渲染。
通常您还需要设置 InterpolationMode.NearestNeighbor
。
关于c# - DrawImage 调整后的图像太小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50419325/
我使用缓冲方法更新 Canvas ,问题是当我在缓冲 Canvas 中绘制图像并将其应用于真实 Canvas 时,真实 Canvas 上没有图像。但我可以将任何其他东西应用到真正的 Canvas 上。
好的,我正在使用 JavaScript Canvas 元素等创建一个游戏。我已经能够加载大量图像,但在选定的少数图像上,JavaScript 会回复错误,例如 Uncaught TypeError:
我的 Canvas 有一个简单的绘制图像,但它不会显示在第一帧上。 这让我发疯,我不知道为什么它不会这样做!! 这是我的脚本: img = new Image(); img.src = 'images
我正在尝试在我的代码上调用 DrawImage(),我正在遵循本教程 Jetpack Tutorial , 但我收到此错误: Unresolved reference: DrawImage 我尝试在
我目前正在尝试将图像打印到屏幕上,以便准备创建游戏。我可以单独显示图像,但前提是该图像是在 paintComponents() 类中实例化的。当然,我不想每帧都重新实例化整个板,但似乎无法让它工作。我
我正在尝试使用drawImage()方法获取要在屏幕上绘制的.jpg图像,但它不会绘制。这是代码: public void paint(Graphics g) { Image imag
在我的程序中,我有以下代码: package io.github.AdmiralSbs.DiceWars; import javax.imageio.ImageIO; import javax.swi
这基本上就是我的代码的工作方式 class Main extends JFrame implements Runnable { public Main() { //init ever
几天来,我一直在努力弄清楚为什么我的九层代码不能按预期工作。据我所知,Graphics.DrawImage 方法似乎存在问题,它无法正确处理我的九个切片图像。所以我的问题是如何补偿在紧凑型框架上运行我
当您将名为 logo.png 的图片放在与此 html 文件相同的目录中并尝试在 Web 浏览器中运行它时,该图片在 IE 中仅出现 10 次刷新中的 1 次,而在Firefox,但在进一步刷新后会出
当我使用 Graphics.DrawImage 绘制图像并以比原始图像更大的尺寸绘制时,它最终变得有点太小了。您可以在下图中看到这一点: 绿线不应该是可见的,也不是图像的一部分。相反,它们被绘制在图像
import java.awt.*; import javax.swing.*; public class Main { JFrame jf; Main() {
我正在尝试使用 Java 面板显示图像,但行不通。该代码没有给出任何异常和/或错误,但图像没有加载。该图像确实存在,我也尝试过 .jpg,但效果不佳。 package feupcraftproject
我需要在内存中保存约 50 个图像(这是必须的,也是我无法改变的条件)。但是,有时我想在 JFrame 上绘制这些图像的缩略图。 使用 graphics.drawImage(picture, 100,
最近我一直在构建一个在JFrame上绘制图像的应用程序,一切都很棒,但是当窗口离开屏幕时,绘制的图像就消失了,我一直想知道是否有办法避免那?谢谢 import javax.swing.*; 导入 ja
我有一个扩展 jlabel 并使用 paintComponent 在其上绘制的类,如下所示这是 paintPhotos.java package myApp; import java.awt.*; i
我正在玩 JavaFX,在 start(Stage theStage) 类中我有以下代码: /*... Scene, stage, canvas ...*/ GraphicsContext gc =
我正在尝试编写一些简单的游戏,但我一直在 Canvas 上绘制资源。我创建了一个 AssetsLoader“类”来加载图像并将其保存为图像对象。一开始我只想绘制一些加载的资源。但是drawImage函
我使用drawImage来“调整”图像的大小; img.onload = function(){ var width = img.width * 0.16,
我正在使用drawImage()。这是我的代码。 imDiv 包含一个内联 svg。 var c =document.getElementById( 'cvs' ); var ctx =c.g
我是一名优秀的程序员,十分优秀!