Height of String or BMP in pixels .......... Urgent ....... Urgent

M

Muhammad Arif

Hi,
I am developing an application for PDA for Windows CE environment. I
am using Bitmap and Graphics object to create a bitmap. This BMP will
have a 3x3 matrix on screen and have to place some text or image in
each cell of the matrix. The height of text or image may vary and
that's why I need to calculate the height of a string before drawing
so that I can have the coordinates of next cell on screen.

I just need to know the following two things:
1- Is there any API or function/procedure in .NET CF library which
return me the height of a string in pixels or any other unit?

2- Same with the physical bitmap file (.BMP file) i.e, any API or
function/procedure which return me the height of stored .BMP file?

Regards
Arif
 
C

chris-s

Firstly, slow down, the only thing urgent in life is getting to the bar
before closing time...

To measure a string check out the Graphics.MeasureString method.

To measure a bmp, you will almost certainly have to load it into a
System.Drawing.Bitmap class which has the size properties you require.

Chris
 
G

Geoff Schwab [MSFT]

If you do not want to load the entire bitmap then your only other option is
to parse the file yourself or do some weird calculation based on the size of
the file and the headers. You can open the file and then read in the
BITMAPFILEHEADER and BITMAPINFOHEADER and get the information you want.

Try this...

protected class BITMAPFILEHEADER
{
protected const ushort fBitmapFileDesignator = 19778;
protected const uint fBitmapFileOffsetToData = 54;

public ushort bfType;
public uint bfSize;
public short bfReserved1;
public short bfReserved2;
public uint bfOffBits;

public void ReadStream(BinaryReader rdr)
{
bfType = rdr.ReadUInt16();
bfSize = rdr.ReadUInt32();
bfReserved1 = rdr.ReadInt16();
bfReserved2 = rdr.ReadInt16();
bfOffBits = rdr.ReadUInt32();
}
}

protected class BITMAPINFOHEADER
{
const uint kBitmapInfoHeaderSize = 40;

public uint biSize;
public int biWidth;
public int biHeight;
public short biPlanes;
public short biBitCount;
public uint biCompression;
public uint biSizeImage;
public int biXPelsPerMeter;
public int biYPelsPerMeter;
public uint biClrUsed;
public uint biClrImportant;

public void ReadStream(BinaryReader rdr)
{
biSize = rdr.ReadUInt32();
biWidth = rdr.ReadInt32();
biHeight = rdr.ReadInt32();
biPlanes = rdr.ReadInt16();
biBitCount = rdr.ReadInt16();
biCompression = rdr.ReadUInt32();
biSizeImage = rdr.ReadUInt32();
biXPelsPerMeter = rdr.ReadInt32();
biYPelsPerMeter = rdr.ReadInt32();
biClrUsed = rdr.ReadUInt32();
biClrImportant = rdr.ReadUInt32();

}
}

Now you can load the file like this...

BITMAPFILEHEADER fileHdr = new BITMAPFILEHEADER();
BITMAPINOHEADER infoHdr = new BITMAPINFOHEADER();

FileStream fs = new FileStream(fileName, FileMode.Open);
BinaryReader br = new BinaryReader(fs);

fileHdr.ReadStream(br);
infoHdr.ReadStream(br);

br.Close();
fs.Close();

The bitmap size is in infoHdr.biWidth and infoHdr.biHeight.

--
Geoff Schwab
Program Manager
Excell Data Corporation
http://msdn.com/mobility
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.aspx

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Muhammad Arif

chris-s said:
Firstly, slow down, the only thing urgent in life is getting to the bar
before closing time...

To measure a string check out the Graphics.MeasureString method.

To measure a bmp, you will almost certainly have to load it into a
System.Drawing.Bitmap class which has the size properties you require.

Chris


Dear Chris,
Thank you for providing me help regarding my problem of height of
image or string. I have solved both the issues. But there is one more
small problem.

I write a string through .DrawString method of Graphics object. I also
draw a rectangle with .DrawRectangle method of Graphics object using
an other object of RectangleF class to wrap the text in rectangle if
it cross the limit of rectangle.
The PROBLEM is that, the .MeasureString method return only the height
of single line of string not the height of complete wraped string
which is drawn in 2 or 3 lines.

Following is the code of which show the area of my problem


l_objRectangleF = New RectangleF(objAxis.ULX, objAxis.ULY, 224, 40)
l_objSizeF = New SizeF

l_objGraphics.DrawRectangle(m_objPenBlack, objAxis.ULX, objAxis.ULY,
224, 0)
l_objGraphics.DrawString("my str", m_objFont, m_objBrushBlack,
l_objRectangleF)
l_objSizeF = l_objGraphics.MeasureString("my str", m_objFont)



Waiting for your positive responce again.
Arif
 
X

Xin Yan

Arif,
Graphics.MeasureString doesn't support passing size and stringformat as
parameter. In this case, can you call MeasureString first, calculate how
many lines you need, and multiply the height by the number of lines you got?
hope this helps
xin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top