将图像绘制到更大的位图

发布于 2024-09-26 12:51:53 字数 249 浏览 0 评论 0原文

基本上我想在没有空间或黑色背景的情况下拉伸较小的图像(即300x300到更大的图像,即500x500)。

我有一个位图(假设宽度为 500px,高度为 500px)。如何在该位图上绘制另一个(较小的)图像,以便它占据整个位图?

我已经知道如何创建位图(即 var bitmap = new Bitmap(500, 500);)并获取图像 - 它可以从文件加载(即 var image = Image.FromFile (...);) 或从其他来源获得。

Basically I want to stretch smaller image (i.e. 300x300 to bigger one i.e. 500x500) without space or black background.

I have a bitmap (let's say width 500px, and height 500px). How to draw another (smaller) image on that bitmap so it takes whole bitmap?

I already know how to create bitmap (i.e. var bitmap = new Bitmap(500, 500);) and get the image - it can be loaded from file (i.e. var image = Image.FromFile(...);) or obtained from some other source.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不吻晚风 2024-10-03 12:51:53

请参阅 Graphics.DrawImage 的文档。您可以指定源矩形和目标矩形。

示例代码:

Image i = Image.FromFile(fileName); // This is 300x300
Bitmap b = new Bitmap(500, 500);

using(Graphics g = Graphics.FromImage(b))
{
    g.DrawImage(i, 0, 0, 500, 500);
}

要使用代码,请确保添加对 System.Drawing 程序集的引用,并将 using System.Drawing 添加到文件中。

See the documentation for Graphics.DrawImage. You can specify source and destination rectangles.

Example code:

Image i = Image.FromFile(fileName); // This is 300x300
Bitmap b = new Bitmap(500, 500);

using(Graphics g = Graphics.FromImage(b))
{
    g.DrawImage(i, 0, 0, 500, 500);
}

To use code make sure to add reference to System.Drawing assembly and add using System.Drawing to the file.

傾旎 2024-10-03 12:51:53

您可以尝试使用以下方法:

public Image ImageZoom(Image image, Size newSize)
{
    var bitmap = new Bitmap(image, newSize.Width, newSize.Height);
    using (var g = Graphics.FromImage(bitmap))
    {
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    }

    return bitmap;
}

并从可用的 插值模式

You could try using the following:

public Image ImageZoom(Image image, Size newSize)
{
    var bitmap = new Bitmap(image, newSize.Width, newSize.Height);
    using (var g = Graphics.FromImage(bitmap))
    {
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    }

    return bitmap;
}

And choose from one of the available InterpolationModes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文