如何在C#中重复图像 [英] How to repeat an image in C#

查看:95
本文介绍了如何在C#中重复图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张带有特定图案的图像.如何使用GDI在其他图像中重复呢?
在GDI中有什么方法可以做到这一点?

I have an image with a certain pattern. How do I repeat it in another image using GDI?
Is there any method to do it in GDI?

推荐答案

在C#中,您可以创建一个TextureBrush,它将在您使用的任何位置平铺图像,然后用它填充一个区域.像这样(一个填充整个图像的示例)...

In C#, you can create a TextureBrush that'll tile your image wherever you use it, and then fill an area with it. Something like this (an example that fills the whole image)...

// Use `using` blocks for GDI objects you create, so they'll be released
// quickly when you're done with them.
using (TextureBrush brush = new TextureBrush(yourImage, WrapMode.Tile))
using (Graphics g = Graphics.FromImage(destImage))
{
    // Do your painting in here
    g.FillRectangle(brush, 0, 0, destImage.Width, destImage.Height);
}

注意,如果要控制图像的平铺方式,则需要学习一些有关变换的知识.

Note, if you want some control over how the image is tiled, you're going to need to learn a bit about transforms.

我差点忘了(实际上我确实忘记了一点):您需要导入System.Drawing(对于GraphicsTextureBrush)和System.Drawing.Drawing2D(对于WrapMode)以获取代码.可以按原样工作.

I almost forgot (actually I did forget for a bit): You'll need to import System.Drawing (for Graphics and TextureBrush) and System.Drawing.Drawing2D (for WrapMode) in order for the code above to work as is.

这篇关于如何在C#中重复图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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