Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / multimedia / GDI+

How to Create a Nokia OTA Picture Message

4.10/5 (7 votes)
26 Oct 2006CPOL 1   869  
How to create a mobile OTA picture message.

Intrdoduction

This sample shows how to create picture messages (*.ota files) for mobiles. OTA files are 72 x 28 pixel bitmaps.

This sample also converts Windows black-and-white, 72 x 28 pixel bitmap files to OTA files.

C#
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
namespace OTAViewer
{
    public class OTAFile
    {
        public const int WIDTH = 72;
        public const int HEIGHT = 28;
        private const int DATASIZE = WIDTH / 8 * HEIGHT;
        byte[,] m_Bitmap = 
        new byte[WIDTH / 8, HEIGHT];
     
        private OTAFile()
        {
        }

        /// <summary>
        /// Represents a (71, 27) Bitmap.
        /// </summary>
        /// <param name="x">X</param>
        /// <param name="y">Y</param>
        /// <returns>True if Pixel is set.</returns>
        public bool this[int x, int y]
        {
            get
            {
                byte b = m_Bitmap[x / 8, y];
                byte bit = (byte)(7 - x % 8);
                byte t = (byte)Math.Pow(2, bit);
                return (b & t) == 0;
            }
            set
            {
                int xx = x / 8;
                byte bit = (byte)(7 - x % 8);
                byte t = (byte)(1 << bit);
                byte i = (byte)(byte.MaxValue - t);
                if (value)
                {
                    m_Bitmap[xx, y] |= t;
                }
                else
                {
                    m_Bitmap[xx, y] &= i;
                }
            }
        }

        public Bitmap ToBitmap()
        {
            Bitmap bmp = new 
            Bitmap(72, HEIGHT);
            for (int i = 0; i < 72; i++)
            {
                for (int j = 0; j < HEIGHT; j++)
                {
                    if (this[i, j])
                    {
                        bmp.SetPixel(i, j, Color.White);
                    }
                    else
                    {
                        bmp.SetPixel(i, j, Color.Black);
                    }
                }
            }
            return bmp;
        }

        public byte[] Data
        {
            get
            {
                byte[] s = new byte[DATASIZE + 4];
                s[0] = 0;
                s[1] = 72;
                s[2] = HEIGHT;
                s[3] = 1;
                int k = 4;
                for (int j = 0; j < HEIGHT; j++)
                {
                    for (int i = 0; i < 9; i++)
                    {
                        s[k++] = m_Bitmap[i, j];
                    }
                }
                return s;
            }
        }

        public static OTAFile FromBitmap(Image img)
        {
            if (img.Width != 72 || 
              img.Height != HEIGHT || img.PixelFormat != 
              System.Drawing.Imaging.PixelFormat.Format1bppIndexed)
            {
                throw new Exception(string.Format("A " + 
                  "72x{0} Pixel Black-and-White image Required.", 
                  HEIGHT));
            }
            Bitmap bmp = new Bitmap(img);
            OTAFile ota = new OTAFile();
            for (int i = 0; i < 72; i++)
            {
                for (int j = 0; j < HEIGHT; j++)
                {
                    ota[i, j] = (bmp.GetPixel(i, j) != 
                      Color.FromArgb(255, 255, 255));
                }
            }
            return ota;
        }

        public static OTAFile FromFile(string file)
        {
            OTAFile ota = new OTAFile();
            byte[] buf = new byte[DATASIZE];
            Stream fs = File.OpenRead(file);
            fs.Position = 4;
            fs.Read(buf, 0, DATASIZE);
            fs.Close();
            int x;
            int y;
            for (int i = 0; i < DATASIZE; i++)
            {
                x = i % 9;
                y = i / 9;
                ota.m_Bitmap[x, y] = buf[i];
            }
            return ota;
        }
    }
}

Have fun!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)