Click here to Skip to main content
16,013,642 members
Articles / Multimedia / GDI+
Article

How to Create a Nokia OTA Picture Message

Rate me:
Please Sign up or sign in to vote.
4.10/5 (7 votes)
26 Oct 2006CPOL 55.9K   869   13   5
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)


Written By
Team Leader
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalhi Pin
syed umair 24-Feb-10 21:55
syed umair 24-Feb-10 21:55 
Generalbashir Pin
bashir22223-Aug-09 7:46
bashir22223-Aug-09 7:46 
Rose | [Rose] Thumbs Up | :thumbsup: bashir loves rube
NewsWELCOME TO Pin
noushadali3-May-08 1:48
noushadali3-May-08 1:48 
QuestionHow to Synch mobile with website Pin
chcs14-Mar-08 1:02
chcs14-Mar-08 1:02 
GeneralReformat Pin
Jonathan [Darka]26-Oct-06 22:05
professionalJonathan [Darka]26-Oct-06 22:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.