Tuesday, June 30, 2009

How to convert MP3 files to WAV files in .NET (C#)

I have successfully converted compressed audio to PCM files with the Alvas.Audio .NET library. Its FAQ shows how to convert a WAV to a MP3 but not the contrary. So I had to write my own code:

static void Main(string[] args) { string mp3File = @"Theme - Simpsons.mp3"; string wavFile = @"Theme - Simpsons.wav"; using (var wr = new Mp3Reader(File.OpenRead(mp3File))) { IntPtr mp3Format = wr.ReadFormat(); byte[] mp3Data = wr.ReadData(); var wavFormat = AudioCompressionManager.GetWaveFormat(mp3Format); Decode2Pcm(ref mp3Format, ref mp3Data, ref wavFormat); MemoryStream ms = new MemoryStream(); using (var output = File.OpenWrite(wavFile)) using (WaveWriter ww = new WaveWriter(output, AudioCompressionManager.FormatBytes(AudioCompressionManager.GetFormatList(wavFormat)[0].FormatHandle))) { ww.WriteData(mp3Data); } } } private static void Decode2Pcm(ref IntPtr format, ref byte[] data, ref WaveFormat wf) { IntPtr newFormat = AudioCompressionManager.GetCompatibleFormat(format, AudioCompressionManager.PcmFormatTag); byte[] buffer = AudioCompressionManager.Convert(format, newFormat, data, false); wf = AudioCompressionManager.GetWaveFormat(newFormat); format = newFormat; data = buffer; }

Thursday, June 11, 2009

Google AppEngine will reduce free quotas to 14%

Google published on its AppEngine blog the plan to reduce the free use quotas to 6.5 CPU-hours and 1.07 gigabytes of outbound transfer, by June 22nd. It's 14% of the current free CPU time quota and 10% of the current bandwith, and 8,000 of the currently free deployed applications will be charged.

Current free quotas:

46.30 CPU hours
10.00 GBytes Outgoing Bandwith
10.00 GBytes Incoming Bandwith
1.00 GBytes Stored Data
2000 Recipients Emailed

Saturday, June 6, 2009

How to draw on a Windows Form using C# .NET

1. Create a new Windows Forms Application on Visual Studio
2. In the design mode click on the form
3. On the properties window click on events
4. Double click the "Paint" event

and the final step:

5. Put the code below in the file that opens:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Bitmap DrawingArea;  // Area to draw on.

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DrawingArea = new Bitmap(
                    this.ClientRectangle.Width,
                    this.ClientRectangle.Height,
                    System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            using (var canvas = Graphics.FromImage(DrawingArea))
            {
                canvas.Clear(Color.Transparent);
                canvas.FillPolygon(Brushes.Black, new PointF[] { new PointF(10, 10), new PointF(0, 0), new PointF(0, 10) });
            }
            this.Invalidate();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(DrawingArea, 0, 0, DrawingArea.Width, DrawingArea.Height);
        }
    }
}

Wednesday, June 3, 2009

How to convert a Brush to Color in C#

Or How to get the Color of a Brush object in .NET

The solution is pretty simple:
  
 new Pen(brush).Color

The reverse is also possible

 new SolidBrush(color)

Monday, June 1, 2009

Object Oriented Javascript, C# Comparison

C# Code:

public class ClassName : BaseClassName
{
public object PropertyName { get; set; }

public BaseClassName(object argumentName)
{
this.PropertyName = argumentName;
}

public void MethodName()
{
}
}

Javascript Code:

function ClassName(argumentName) {
this.PropertyName = argumentName;
}

ClassName.prototype.MethodName = function() {
}