C# 使用MSDN示例绘制带有gif动画的错误矩形
我有一个gif动画。因为我在CompactFramework中工作,所以我遵循了 基本思想是将帧添加到图像中,并用下一帧重新绘制一个C# 使用MSDN示例绘制带有gif动画的错误矩形,c#,compact-framework,animated-gif,rectangles,C#,Compact Framework,Animated Gif,Rectangles,我有一个gif动画。因为我在CompactFramework中工作,所以我遵循了 基本思想是将帧添加到图像中,并用下一帧重新绘制一个矩形,从而创建动画。它工作得很好,只是动画显示在一个正方形中,而不是我的图像的完整大小(240320) 这是我的代码: 在类AnimateCtl.cs中: public class AnimateCtl : System.Windows.Forms.Control { Timer fTimer; int frameWidth = 240; i
矩形
,从而创建动画。它工作得很好,只是动画显示在一个正方形中,而不是我的图像的完整大小(240320)
这是我的代码:
在类AnimateCtl.cs中:
public class AnimateCtl : System.Windows.Forms.Control
{
Timer fTimer;
int frameWidth = 240;
int frameHeight = 320;
int loopCount = 0;
int loopCounter = 0;
int frameCount;
int currentFrame = 0;
Graphics graphics;
private Bitmap bitmap;
public Bitmap Bitmap
{
get
{
return bitmap;
}
set
{
bitmap = value;
}
}
private void Draw(int iframe)
{
//Calculate the left location of the drawing frame
int XLocation = iframe * frameWidth;
Rectangle rect = new Rectangle(XLocation, 0, frameWidth, frameHeight);
//Draw image
graphics.DrawImage(bitmap, 0, 0, rect, GraphicsUnit.Pixel);
}
public AnimateCtl()
{
//Cache the Graphics object
graphics = this.CreateGraphics();
//Instantiate the Timer
fTimer = new System.Windows.Forms.Timer();
//Hook up to the Timer's Tick event
fTimer.Tick += new System.EventHandler(this.timer1_Tick);
}
/// <summary>
/// Start animation
/// </summary>
/// <param name="frWidth"></param>
/// <param name="DelayInterval"></param>
/// <param name="LoopCount"></param>
public void StartAnimation(int frWidth, int DelayInterval, int LoopCount)
{
frameWidth = frWidth;
//How many times to loop
loopCount = LoopCount;
//Reset loop counter
loopCounter = 0;
//Calculate the frameCount
frameCount = bitmap.Width / frameWidth;
frameHeight = bitmap.Height;
//Resize the control
//this.Size(frameWidth, frameHeight);
//Assign delay interval to the timer
fTimer.Interval = DelayInterval;
//Start the timer
fTimer.Enabled = true;
}
private void timer1_Tick(object sender, System.EventArgs e)
{
if (loopCount == -1) //loop continuously
{
this.DrawFrame();
}
else
{
if (loopCount == loopCounter) //stop the animation
fTimer.Enabled = false;
else
this.DrawFrame();
}
}
private void DrawFrame()
{
if (currentFrame < frameCount - 1)
{
//move to the next frame
currentFrame++;
}
else
{
//increment the loopCounter
loopCounter++;
currentFrame = 0;
}
Draw(currentFrame);
}
为什么它没有以正确的尺寸绘制矩形?
谢谢你的帮助 删除Draw methode并像这样更新DrawFrame
private void DrawFrame() {
if (currentFrame < frameCount - 1) {
currentFrame++;
} else {
loopCounter++;
currentFrame = 0;
}
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
int XLocation = currentFrame * frameWidth;
Rectangle rect = new Rectangle(XLocation, 0, frameWidth, frameHeight);
e.Graphics.DrawImage(bitmap, 0, 0, rect, GraphicsUnit.Pixel);
}
private void DrawFrame(){
如果(当前帧<帧计数-1){
currentFrame++;
}否则{
loopCounter++;
currentFrame=0;
}
这个。使无效();
}
受保护的覆盖无效OnPaint(PaintEventArgs e){
基础漆(e);
int XLocation=当前帧*帧宽;
矩形rect=新矩形(XLocation,0,frameWidth,frameHeight);
e、 Graphics.DrawImage(位图、0、0、rect、GraphicsUnit.Pixel);
}
永远不要使用这个.CreateGraphics()。覆盖OnPaint方法是正确的方法。调用此函数后,OnPaint将始终激发。Invalidate()
使用此.CreateGraphics()只能获得当前的图形大小,但在窗体显示后,控件大小可能会更改。删除绘图方法并像这样更新绘图框
private void DrawFrame() {
if (currentFrame < frameCount - 1) {
currentFrame++;
} else {
loopCounter++;
currentFrame = 0;
}
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
int XLocation = currentFrame * frameWidth;
Rectangle rect = new Rectangle(XLocation, 0, frameWidth, frameHeight);
e.Graphics.DrawImage(bitmap, 0, 0, rect, GraphicsUnit.Pixel);
}
private void DrawFrame(){
如果(当前帧<帧计数-1){
currentFrame++;
}否则{
loopCounter++;
currentFrame=0;
}
这个。使无效();
}
受保护的覆盖无效OnPaint(PaintEventArgs e){
基础漆(e);
int XLocation=当前帧*帧宽;
矩形rect=新矩形(XLocation,0,frameWidth,frameHeight);
e、 Graphics.DrawImage(位图、0、0、rect、GraphicsUnit.Pixel);
}
永远不要使用这个.CreateGraphics()。覆盖OnPaint方法是正确的方法。调用此函数后,OnPaint将始终激发。Invalidate()
使用此.CreateGraphics()只能获得当前的图形大小,但在窗体显示后,控件大小可能会更改。我添加了您的代码,但它会产生相同的结果。事实上,如果我在“e.Graphics.DrawImage”之后调试OnPaint,它将绘制显示正方形的矩形。但矩形的尺寸应为240x320。我不明白为什么会这样。谢谢你的帮助!将控件(animCtl)添加到controlcollection后是否更改其大小?animCtl.Size=新尺寸(240;320);这就是问题所在!我添加了
this.Size=新尺寸(240320)代码>现在它工作正常。非常感谢。我添加了您的代码,但它会产生相同的结果。事实上,如果我在“e.Graphics.DrawImage”之后调试OnPaint,它将绘制显示正方形的矩形。但矩形的尺寸应为240x320。我不明白为什么会这样。谢谢你的帮助!将控件(animCtl)添加到controlcollection后是否更改其大小?animCtl.Size=新尺寸(240;320);这就是问题所在!我添加了this.Size=新尺寸(240320)代码>现在它工作正常。非常感谢。