C#picturebox加载带偏移的图像 [英] C# picturebox load image with an offset

查看:301
本文介绍了C#picturebox加载带偏移的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个图像的资源文件(.png格式)。它们的大小和间距可以根据它们的偏移相对容易地调用。

I have a resource file (in .png format) which contain several images. They are sized and spaced in a way to where they should be relatively easy to call based on their offsets.

我可以调整图片框的大小以适应一个图像就好了;但是,我不知道如何基于其偏移加载图像,所以我总是只得到左上角的那个。

I can size the picturebox to fit one image just fine; however, I don't know how to load the image based on its offsets, so I will always just get the one in the top left.

我真的很好使用几乎任何方法,但无法在我的搜索中找到任何有用的东西 - 因为我真的不知道要搜索什么。

I'm really fine with using just about any method, but haven't been able to turn up with anything useful in my searches -- since I didn't really know what to search for exactly.

推荐答案

假设您的PNG图像是 imgwidth 像素宽并由 n 水平图像组成,您可以试试这个:

Assuming your PNG image is imgwidth pixel wide and composed by n horizontal images, you could try this:

Image imgsrc = Image.FromFile("...."); // your PNG file
Image imgdst = new Bitmap(imgwidth/n, imgsrc.Height);
using (Graphics gr = Graphics.FromImage(imgdst))
{
    gr.DrawImage(imgsrc,
        new RectangleF(0, 0, imgdst.Width, imgdst.Height),
        new RectangleF(imgindex * imgwidth/n, 0, imgwidth/n, imgsrc.Height),
        GraphicsUnit.Pixel);
}

想法是创建一个新图像(imgdst)并在其上绘制您需要的原始图像的一部分。

使用新图像,您可以随心所欲,甚至可以在图片框中绘制。

The idea is to create a new image (imgdst) and draw on it the part of original image you need.
With new image you can do what you please, even draw it in a picturebox.

这篇关于C#picturebox加载带偏移的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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