PictureBox plays gif twice - c#

I'm trying to create add a super simple animated gif to a PictureBox control in WinForms.
And I'm having this super weird issue, that the gif runs twice, even though the gif is created to only run once.
This is the code. I tried adding it through Resources and the Design window, same result.
private string gif = AppDomain.CurrentDomain.BaseDirectory + #"resources\lock.gif";
public App()
{
InitializeComponent();
// picturebox is set to disabled in properties
pbxImage.ImageLocation = gif;
pbxImage.Load();
pbxImage.Enabled = true;
}
I was thinking this would be super simple, but I can't for the life of me figure out why. I'm relatively new to WinForms so I'm guessing there's something I'm missing.

Related

How to show an array of images, without freezing the whole programm for seconds

I have the following problem:
I want to make a little 2d-game in c#.
To display a background that consists of several images. To make animated gifs possible, i used MediaElement objects.
I added these MediaElements to my Canvas.
But when i run my programm, the performance is a disaster. It takes almost half a minute to display the images.
I obviously need a better idea to display an array of images, without slowing down everything.
This is my Starting-Point:
class BackgroundMediaElement : MediaElement
{
public BackgroundMediaElement(string imageSourcePath, int rowInCanvas, int columnInCanvas, int lengthOfSquare)
{
this.UnloadedBehavior = MediaState.Manual;
this.Source = new Uri(imageSourcePath, UriKind.Relative);
Canvas.SetTop(this, rowInCanvas*lengthOfSquare);
Canvas.SetLeft(this, columnInCanvas*lengthOfSquare);
this.Play();
this.MediaEnded += method_MediaEnded;
}
private void method_MediaEnded(object sender, RoutedEventArgs e)
{
//MediaElement m = (MediaElement)sender;
this.Position = TimeSpan.FromMilliseconds(1);
}
}
I created more than 200 elements and added them to my Canvas.
But it even slows down with 40 elements.
One image does not even have one kByte.
This is simply not the technology you are looking for. You can use C# very well to create games, but you should be looking for an appropriate Game Engine. Unity supports 2D games and allows you to code in C#. Click here for more information.
Focus on creating games rather than fighting with irrelevant stuff :)

Need to select a frame in gif

I need to restart a gif image so it will start from the first frame.
I have little experience with gif images in C#. I work on a C# windows form application. My gif image is in a picturebox. I need to restart it each time a person for example presses a button.
I have tried doing:
pictureBox1.Image.SelectActiveFrame(new FrameDimension(pictureBox1.Image.FrameDimensionsList[0]), 0)
but it didnt show any change.
I also tried Image animator also no luck. I really need help it is for my final project in high school engineering.
I found that Image.SelectActiveFrame() doesn't seem to work because how it works is not what you thought. It just set the initial frame if there is some control reading its frame, the first frame read is the active frame. So after SelectActiveFrame() you have to re-assign the Image property of your pictureBox to that new Image, like this:
private void RestartToFrameIndex(int index){
pictureBox.Image.SelectActiveFrame(new FrameDimension(pictureBox.Image.FrameDimensionsList[0]), index);
pictureBox.Image = pictureBox.Image;
}
//If you want to restart to the first frame, just call the method above like this:
RestartToFrameIndex(0);
That's the general solution I've just found :) hope it helps others...
I tried this solution and It's working fine :
Image animated = pictureBox1.Image;
pictureBox1.Image = animated;
this way , you reset pictureBox1.Image so the animated GIF starts from beginning.

Animation for images to fly up from bottom of screen using VS blend

I am creating an app that will allow the user to click on images and then they will transform into something else, and the code to do that is below:
private void Image_Tapped(object sender, TappedRoutedEventArgs e)
{
BitmapImage bitmap = new BitmapImage(new Uri("ms-appx:///Assets/smallImage.png"));
imageHolder.Source = bitmap;
e.Handled = true;
}
Now the hard part is that I have to get these pre-transformed images to fly up onto the screen so the user can rush to click on them before they fall. I figured I would need to create storyboards and add those effects, if that is right word for it, but how exactly would I go about this in blend?
So to make fly out from another image in blend, I created individual storyboards for each image and recorded them flying around in whatever direction I wanted for as long as I needed. So for 3 seconds each and so on. Really easy once I got the hang of it, but hoping for new tutorials on blend for developers!

Windows Phone Navigation transitions delay and blank screen in between

I'm using WP Toolkit to do the transitions between my app pages, it works fine , but I get this strange delay in between transitions while navigating from one page to another, it just shows a blank screen which obviously doesn't look good, without any transition it opens the page straight away without any delay or blank screen. This has taken almost 2 days of my time and I don't know what's wrong, I'd appreciate it if someone can help me with it or suggest another page transition library .
(I tried WP7Contrib transitions but I have the same problem with that, not sure if its my app or the libraries)
In fact the background between transitions is black and to avoid that kind of behavior I solved the issue by setting the background in the App.Xaml.cs
private void InitializePhoneApplication()
{
if (phoneApplicationInitialized)
return;
// Create the frame but don't set it as RootVisual yet; this allows the splash
// screen to remain active until the application is ready to render.
RootFrame = new TransitionFrame();
var brush = new ImageBrush
{
ImageSource = new System.Windows.Media.Imaging.BitmapImage(new Uri("Images/Background.jpg", UriKind.Relative)),
Opacity = 0.8d
};
RootFrame.Background = brush;
RootFrame.Navigated += CompleteInitializePhoneApplication;
// Handle navigation failures
RootFrame.NavigationFailed += RootFrame_NavigationFailed;
// Ensure we don't initialize again
phoneApplicationInitialized = true;
}
With that all my pages have my background and the black background is no longer displayed during the transition.
I would suggest you to create your own slide transitions between pages. Its quite simple actually. Create a storyboard and play them at onNavigatingFrom and onNavigatedTo functions in both the page you are navigating from and page you are going into respectively. It just gives me what and how i wanted in my applications. Removing additional references makes your code more optimized.

.NET code to record a specific form activity + audio to avi

I am looking for some C# code .. or .NET component to record the activity of a form on my screen (not the whole desktop) along with audio, similar to what programs like Camtasia allow to do.
The video part is actually pretty easy. All you need to have is a timer running 20 times a second that will save form's canvas to a image files as frames. Then create an animation out of these pictures.
To capture image:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
tVideo.Start();
}
int i = 0;
private void tVideo_Tick(object sender, EventArgs e)
{
String lFile = String.Format("c:\\{0}.bmp", i);
SaveAsBitmap(this, lFile);
i++;
}
public void SaveAsBitmap(Control aCtrl, string aFileName)
{
if (File.Exists(aFileName))
File.Delete(aFileName);
Graphics lGraphics = aCtrl.CreateGraphics();
Bitmap lImage = new Bitmap(aCtrl.Width, aCtrl.Height);
aCtrl.DrawToBitmap(lImage, new Rectangle(0, 0, aCtrl.Width, aCtrl.Height));
lImage.Save(aFileName);
lImage.Dispose();
}
}
This is just a light sample, of course you would have to add some compression and try to avoid saving the same image twice. Knowing how many images are the same + knowing about the framerate, you know how long to display the same frame.
To add a cursor you would have to keep some variables with mouse x,y and an event on mouse click. And then just add it to pictures.
Of course, this won't work for 3d games inspite of overlay which is drawn after the win32 paints.
For that you would have to go with DirectX/OpenGl/XNA. I think the idea is the same.
For audio, DirectX also.
My complete source:
http://deathsquad.pl/archiwum/Inne/so/answer-1934452.rar
Some DirectX audio samples:
http://www.codeproject.com/KB/directx/audiosav.aspx
Check out Gallio: It is a great testing framework, and it has built in screen recording API.
This post shows a few examples:
http://blog.bits-in-motion.com/2009/09/announcing-gallio-and-mbunit-v31.html

Categories

Resources