Posts Tagged ‘Source Code’

People want something to happen. And me too.
So I made the MP3 player. And it really can play MP3.
With the “MediaElement”, you can play video files too.

mp3_test.jpg (17 KB)

“MainPage.xaml” looks like this.

<UserControl x:Class="MP3MediaTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
  <Grid x:Name="LayoutRoot">
        <StackPanel>
            <MediaElement AutoPlay="False" x:Name="myMP3" Source="Etudo.mp3" />
            <TextBlock FontSize="20" x:Name="myStatus" />
            <StackPanel Orientation="Horizontal">
                <Button x:Name="myPlayButton" Width="100" Content="Play" Click="OnPlay" />
                <Button x:Name="myPauseButton" Width="100" Content="Pause" Click="OnPause" />
                <Button x:Name="myStopButton" Width="100" Content="Stop" Click="OnStop" />
            </StackPanel>
        </StackPanel>
    </Grid>
</UserControl>

And “MainPage.xaml.cs” looks like this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace MP3MediaTest
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            this.myMP3.CurrentStateChanged += new RoutedEventHandler(OnCurrentStateChanged);
        }

        void OnCurrentStateChanged(object sender, RoutedEventArgs e)
        {
            bool isPlaying = (this.myMP3.CurrentState == MediaElementState.Playing);
            this.myPauseButton.IsEnabled = isPlaying && this.myMP3.CanPause;
            this.myPlayButton.IsEnabled = !isPlaying;
            this.myStatus.Text = this.myMP3.CurrentState.ToString() + " - " + this.myMP3.Source.ToString();
        }

        private void OnPlay(object sender, RoutedEventArgs e)
        {
            this.myMP3.Play();
        }

        private void OnPause(object sender, RoutedEventArgs e)
        {
            this.myMP3.Pause();
        }

        private void OnStop(object sender, RoutedEventArgs e)
        {
            this.myMP3.Stop();
        }
    }
}

imagebrush.jpg (36 KB)

<UserControl x:Class="ImageBrushTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
  <Grid x:Name="LayoutRoot">
      <Canvas>
            <Rectangle RadiusX="30" RadiusY="30" Fill="AliceBlue" Stroke="Gray" StrokeThickness="5" Canvas.Left="5" Canvas.Top="5" Width="360" Height="360" />
            <Ellipse Width="330" Height="330" Canvas.Left="20" Canvas.Top="20" Stroke="Coral" StrokeThickness="10">
                <Ellipse.Fill>
                    <ImageBrush Stretch="Uniform" ImageSource="bird.png" />
                </Ellipse.Fill>
            </Ellipse>
        </Canvas>
  </Grid>
</UserControl>

Image file should be in the same folder as the XAP file (“ClientBin” folder). If it is not the case, you must specify the path to the file (ie http://www.yoursite.com/images/bird.png).
Actually, the XAP file is a ZIP file with different extension. You can change its extension and unzip it.

I experimented a few shapes with Silverlight.
If you just want to test in your Visual Studio 2008, copy the code and paste it into your IDE.
You do not need to do anything in C# or VB.

graphic_draw.jpg (41 KB)

<UserControl x:Class="GraphicDraw.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
  <Grid x:Name="LayoutRoot">
        <Canvas>
            <Path Fill="Chocolate" Data="F1 M 75, 15 L 15, 150 L 150, 150" />
            <Rectangle Fill="Blue" Canvas.Left="100" Canvas.Top="160" Width="200" Height="150" />
            <Rectangle Fill="AliceBlue" Stroke="Black" StrokeThickness="10" Canvas.Left="200" Canvas.Top="50" Width="100" Height="100" />
            <Rectangle Fill="AliceBlue" Stroke="Black" StrokeThickness="10" RadiusX="40" RadiusY="40" Canvas.Left="380" Canvas.Top="50" Width="150" Height="200" />
            <Ellipse Fill="Red" Canvas.Left="300" Canvas.Top="260" Width="200" Height="150" />
            <Rectangle Width="500" Height="50" Canvas.Left="10" Canvas.Top="420">
                <Rectangle.Fill>
                    <LinearGradientBrush StartPoint="0, 0" EndPoint="1, 1">
                        <LinearGradientBrush.GradientStops>
                            <GradientStop Color="Yellow" Offset="0"/>
                            <GradientStop Color="Blue" Offset="1"/>
                        </LinearGradientBrush.GradientStops>
                    </LinearGradientBrush>
                </Rectangle.Fill>
            </Rectangle>
        </Canvas>
  </Grid>
</UserControl>

I hope it can be useful for you.

ttf2bmp.jpg (44 KB)

I wanted to make a small game with C# because I got a book, and was looking for a program that could convert the font to BMP or something like that.
I found both shareware and freeware on the Internet, and I was not happy with them. Especially shareware. I did not want to pay for just that. I thought almost do it myself, but I happened to find a good one. Actually on Microsoft’s website. Even better, it’s free. Moreover, with source code in C#. Source is slightly older and you have to convert them but it went well for me.

The text below is from the included HTML file with the source code.

This utility converts Windows TrueType fonts into .bmp files that can be imported using the XNA Framework Content Pipeline, and converted into SpriteFont objects by BitmapFontProcessor.
Overview

When you add a new font description to an XNA Framework project, this creates an XML description file describing what font you want to use. The Content Pipeline reads this description, renders out the appropriate characters using the Windows Font API, and converts them into a format that can be efficiently rendered using the SpriteFont class.

You may find “ttf2bmp.exe” on the web, but I would say that it is only the source code. Anyway what I found on Microsoft’s website.
Sorry I forgot the address. Go to the http://www.microsoft.com/express/ and look for ttf2bmp.exe.