sto*_*oic 7 c# image-processing winforms
我正在尝试创建一个非常基本的精灵图像.
首先,我有一个现有的图像(宽度= 100像素,高度= 100像素).
我将循环显示此图像10到100次,每次将它放在前一个旁边的精灵上.
精灵限制在3000px宽.
将图像放在一起是很好的,因为我可以将它们与一个简单的方法结合起来,但是,我需要将组合图像的宽度限制为3000px,然后从一个新行开始.
以下MSDN文章中有关于2D精灵的大量信息:渲染2D精灵
这些示例基于Microsoft的XNA,这是一个可以在Visual Studio中用于开发Windows,Windows Phone和XBOX 360游戏的平台.
例如,要绘制精灵,您可以使用以下C#代码(示例取自MSDN文章,删除XBOX 360特定代码):
private Texture2D SpriteTexture;
private Rectangle TitleSafe;
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
SpriteTexture = Content.Load<Texture2D>("ship");
TitleSafe = GetTitleSafeArea(.8f);
}
protected Rectangle GetTitleSafeArea(float percent)
{
Rectangle retval = new Rectangle(
graphics.GraphicsDevice.Viewport.X,
graphics.GraphicsDevice.Viewport.Y,
graphics.GraphicsDevice.Viewport.Width,
graphics.GraphicsDevice.Viewport.Height);
return retval;
}
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
Vector2 pos = new Vector2(TitleSafe.Left, TitleSafe.Top);
spriteBatch.Draw(SpriteTexture, pos, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
Run Code Online (Sandbox Code Playgroud)
你需要调用LoadContent()初始化它,然后你需要调用GetTitleSafeArea(100)以获得安全绘制区域(在这种情况下是100%),最后你可以使用该Draw方法.它接受包含GameTime类实例的参数,该实例是游戏计时状态的快照,以可由变步(实时)或固定步(游戏时间)游戏使用的值表示.
如果有帮助,请告诉我.
让我尝试一些伪代码:
Bitmap originalImage; // that is your image of 100x100 pixels
Bitmap bigImage; // this is your 3000x3000 canvas
int xPut = 0;
int yPut = 0;
int maxHeight = 0;
while (someExitCondition)
{
Bitmap imagePiece = GetImagePieceAccordingToSomeParameters(originalImage);
if (xPut + imagePiece.Width > 3000)
{
xPut = 0;
yPut += maxHeight;
maxHeight = 0;
}
DrawPieceToCanvas(bigImage, xPut, yPut, imagePiece);
xPut += imagePiece.Width;
if (imagePiece.Height > maxHeight) maxHeight = imagePiece.Height;
// iterate until done
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7967 次 |
| 最近记录: |