使用图片框循环旋转图像 [英] Rotate image in loop using picture box

查看:115
本文介绍了使用图片框循环旋转图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI伙伴们



我正在建立一个德州扑克游戏,因为我进入vb并学习项目。

我有3个图片框持有刻录卡。



我发现这个简洁的代码,看了之后我就明白了。



它的作用是让你设置一个角度来旋转你的图像。



当我插入我的图片框的名字时效果很好。 />


pbBurn1.image

pbBurn2.image

pbBurn3.image



我试图使用相同的方法循环所有三个图像并将它们全部旋转-45度。

代码中的错误似乎没有按预期运行,但没有输出任何内容。你可以帮忙吗...吼我发了双重评论代码我尝试添加循环,从来没有用过,有人能告诉我我做错了什么。



< b>我尝试了什么:



HI guys

I am building a Texas holdem game as my get into vb and learn project.
I have 3 picture boxes that hold the burn cards.

I found this neat code that after looking it over so much I kind of understand.

What it does is lets you set an angle to rotate your image.

Works great when i plug in the names of my picture boxes.

pbBurn1.image
pbBurn2.image
pbBurn3.image

I was trying to use the same method to loop thru all three images and rotate them all -45 degrees.
No errors in code seems to run thru instructions as expected but outputs nothing..can you please help...bellow I placed double comments on the code i tried adding to loop that never worked can someone tell me what i did wrong.

What I have tried:

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

        ''Dim BurnNum As Image = pbBurn1.Image
        ''Dim t As Integer = 1
        ''Dim count As Integer = 1
        ''For t = 1 To 3

        ''    If count = 2 Then BurnNum = pbBurn2.Image
        ''    If count = 3 Then BurnNum = pbBurn3.Image

        ' Copy the output bitmap from the source image.
        Dim bm_in As New Bitmap(pbBurn2.Image)  '' Dim bm_in As New Bitmap(BurnNum)

        ' Make an array of points defining the
        ' image's corners.
        Dim wid As Single = bm_in.Width
        Dim hgt As Single = bm_in.Height
        Dim corners As Point() = { _
            New Point(0, 0), _
            New Point(wid, 0), _
            New Point(0, hgt), _
            New Point(wid, hgt)}

        ' Translate to center the bounding box at the origin.
        Dim cx As Single = wid / 2
        Dim cy As Single = hgt / 2
        Dim i As Long
        For i = 0 To 3
            corners(i).X -= cx
            corners(i).Y -= cy
        Next i

        ' Rotate.
        Dim theta As Single = Single.Parse(-45) * PI / 180.0
        Dim sin_theta As Single = Sin(theta)
        Dim cos_theta As Single = Cos(theta)
        Dim X As Single
        Dim Y As Single
        For i = 0 To 3
            X = corners(i).X
            Y = corners(i).Y
            corners(i).X = X * cos_theta + Y * sin_theta
            corners(i).Y = -X * sin_theta + Y * cos_theta
        Next i

        ' Translate so X >= 0 and Y >=0 for all corners.
        Dim xmin As Single = corners(0).X
        Dim ymin As Single = corners(0).Y
        For i = 1 To 3
            If xmin > corners(i).X Then xmin = corners(i).X
            If ymin > corners(i).Y Then ymin = corners(i).Y
        Next i
        For i = 0 To 3
            corners(i).X -= xmin
            corners(i).Y -= ymin
        Next i

        ' Create an output Bitmap and Graphics object.
        Dim bm_out As New Bitmap(CInt(-2 * xmin), CInt(-2 * ymin))
        Dim gr_out As Graphics = Graphics.FromImage(bm_out)

        ' Drop the last corner lest we confuse DrawImage, 
        ' which expects an array of three corners.
        ReDim Preserve corners(2)

        ' Draw the result onto the output Bitmap.
        gr_out.DrawImage(bm_in, corners)

        ' Display the result.
        pbBurn2.Image = bm_out '' Burnnum = bm_out

        ''count = count + 1
        ''Next

    End Sub

推荐答案

图像没有动画的原因是因为你的应用程序在通常称为UI线程的东西上启动。此线程侦听进入消息队列的消息并将这些消息处理为事件并处理与UI相关的所有内容,例如重绘控件和窗口。



问题在于您的代码正在占用UI线程并阻止它处理WM_PAINT消息,这些消息将告诉您的图片框用新图像重新绘制自己。



而不是在循环最好使用计时器并保持图像状态,旋转它并告诉图片框使用新图像。一旦这个代码结束了这个时间的变化,或者计时器的滴答,UI线程就可以处理WM_PAINT消息,并且图片框可以重新绘制新图像。



实际上,您根本不应该使用PictureBox控件,只需在Form表面或Panel控件上使用Paint事件自行绘制所有内容。
The reason the image doesn't animate is because your application starts on what is normally called the "UI Thread". This thread listens for messages coming into the message queue and processes these messages into events and handles everything UI related, like redrawing controls and windows.

The problem is that your code is hogging the UI thread and blocking it from processing the WM_PAINT messages that are coming in to tell your picturebox to repaint itself with the new image.

Instead of doing it in a loop it's better to use a timer and maintain a state of the image, rotating it and telling the picturebox to use the new image. Once this code ends for this one change in time, or tick of the timer, the UI thread can process the WM_PAINT message and the picturebox can repaint with the new image.

Really, you shouldn't be using PictureBox controls at all and just paint everything yourself on either the surface of the Form or on a Panel control using the Paint event for them.


这篇关于使用图片框循环旋转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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