Attempted to read or write protected memory.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im trying to use a library dll to call a function to extract ascii text then convert to bmp and save to a file. the text field, font number and hue are defined by comboBox entries.
main script:

<div style="color:black; background-color:white
<pre><span style="color:blue using System;
<span style="color:blue using System.Collections.Generic;
<span style="color:blue using System.ComponentModel;
<span style="color:blue using System.Data;
<span style="color:blue using System.Drawing;
<span style="color:blue using System.Drawing.Imaging;
<span style="color:blue using System.Linq;
<span style="color:blue using System.Text;
<span style="color:blue using System.IO;
<span style="color:blue using System.Windows.Forms;
<span style="color:blue using Ultima;

<span style="color:blue namespace BitmapUOFont
{
<span style="color:blue public <span style="color:blue partial <span style="color:blue class Form1 : Form
{
<span style="color:blue public Form1()
{
InitializeComponent();
}

<span style="color:blue private <span style="color:blue void label1_Click(<span style="color:blue object sender, EventArgs e)
{

}

<span style="color:blue private <span style="color:blue void comboFontNumber_SelectedIndexChanged(<span style="color:blue object sender, EventArgs e)
{
}

<span style="color:blue private <span style="color:blue void comboBox1_SelectedIndexChanged(<span style="color:blue object sender, EventArgs e)
{
}

<span style="color:blue private <span style="color:blue void openFileDialog1_FileOk(<span style="color:blue object sender, CancelEventArgs e)
{

}

<span style="color:blue private <span style="color:blue void textBox1_TextChanged(<span style="color:blue object sender, EventArgs e)
{
}

<span style="color:blue private <span style="color:blue void btnSave_Click(<span style="color:blue object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = <span style="color:blue new SaveFileDialog();
saveFileDialog1.Filter = <span style="color:#a31515 "Bitmap Image|*.bmp";
saveFileDialog1.Title = <span style="color:#a31515 "Save an Image File";
saveFileDialog1.ShowDialog();

<span style="color:blue if (saveFileDialog1.FileName != <span style="color:#a31515 "")
{
<span style="color:blue int fontnumber = comboFontNumber.SelectedIndex;
<span style="color:blue string entry = textBox1.Text;
<span style="color:blue int huenum = comboBox1.SelectedIndex;
<span style="color:blue short sHue = Convert.ToInt16(huenum);

<span style="color:blue using (Bitmap bmp = Ultima.ASCIIText.DrawText(fontnumber, <span style="color:#a31515 entry, sHue))
{
<span style="color:blue byte[] buffer = <span style="color:blue new <span style="color:blue byte[0];

<span style="color:blue using (MemoryStream ms = <span style="color:blue new MemoryStream())
{
bmp.Save(ms, ImageFormat.Png);
}
}
}
}

<span style="color:blue private <span style="color:blue void saveFileDialog1_FileOk(<span style="color:blue object sender, CancelEventArgs e)
{

}
}
}
[/code]


Here is the method script from the dll

<div style="color:black; background-color:white
<pre><span style="color:blue using System;
<span style="color:blue using System.IO;
<span style="color:blue using System.Drawing;
<span style="color:blue using System.Drawing.Imaging;
<span style="color:blue using System.Text;
<span style="color:blue namespace Ultima
{
<span style="color:blue public <span style="color:blue sealed <span style="color:blue class ASCIIFont
{
<span style="color:blue private <span style="color:blue int m_Height;
<span style="color:blue private Bitmap[] m_Characters;

<span style="color:blue public <span style="color:blue int Height { <span style="color:blue get { <span style="color:blue return m_Height; } <span style="color:blue set { m_Height = value; } }
<span style="color:blue public Bitmap[] Characters { <span style="color:blue get { <span style="color:blue return m_Characters; } <span style="color:blue set { m_Characters = value; } }

<span style="color:blue public ASCIIFont()
{
Height = 0;
Characters = <span style="color:blue new Bitmap[ 224 ];
}

<span style="color:blue public Bitmap GetBitmap( <span style="color:blue char character )
{
<span style="color:blue return m_Characters[ ( ( ( ( (<span style="color:blue int)character ) - 0x20 ) & 0x7FFFFFFF ) % 224 ) ];
}

<span style="color:blue public <span style="color:blue int GetWidth( <span style="color:blue string text )
{
<span style="color:blue if( text == <span style="color:blue null || text.Length == 0 ) { <span style="color:blue return 0; }

<span style="color:blue int width = 0;

<span style="color:blue for( <span style="color:blue int i = 0; i < text.Length; ++i )
{
width += GetBitmap( text[ i ] ).Width;
}

<span style="color:blue return width;
}

<span style="color:blue public <span style="color:blue static ASCIIFont GetFixed( <span style="color:blue int font )
{
<span style="color:blue if( font < 0 || font > 9 )
{
<span style="color:blue return ASCIIText.Fonts[ 3 ];
}

<span style="color:blue return ASCIIText.Fonts[ font ];
}
}

<span style="color:blue public <span style="color:blue static <span style="color:blue class ASCIIText
{
<span style="color:blue private <span style="color:blue static ASCIIFont[] m_Fonts = <span style="color:blue new ASCIIFont[10];

<span style="color:blue public <span style="color:blue static ASCIIFont[] Fonts { <span style="color:blue get { <span style="color:blue return ASCIIText.m_Fonts; } <span style="color:blue set { ASCIIText.m_Fonts = value; } }

<span style="color:blue static ASCIIText()
{
<span style="color:blue string path = <span style="color:#a31515 @"fonts.mul";

<span style="color:blue if( path != <span style="color:blue null )
{
<span style="color:blue using( BinaryReader reader = <span style="color:blue new BinaryReader( <span style="color:blue new FileStream( path, FileMode.Open ) ) )
{
<span style="color:blue for( <span style="color:blue int i = 0; i < 10; ++i )
{
m_Fonts[ i ] = <span style="color:blue new ASCIIFont();

<span style="color:blue byte header = reader.ReadByte();

<span style="color:blue for( <span style="color:blue int k = 0; k < 224; ++k )
{
<span style="color:blue byte width = reader.ReadByte();
<span style="color:blue byte height = reader.ReadByte();
reader.ReadByte(); <span style="color:green // delimeter?

<span style="color:blue if( width > 0 && height > 0 )
{
<span style="color:blue if( height > m_Fonts[ i ].Height && k < 96 )
{
m_Fonts[ i ].Height = height;
}

Bitmap bmp = <span style="color:blue new Bitmap( width, height );

<span style="color:blue for( <span style="color:blue int y = 0; y < height; ++y )
{
<span style="color:blue for( <span style="color:blue int x = 0; x < width; ++x )
{
<span style="color:blue short pixel = (<span style="color:blue short)( reader.ReadByte() | ( reader.ReadByte() << 8 ) );

<span style="color:blue if( pixel != 0 )
{
bmp.SetPixel( x, y, Color.FromArgb( ( pixel & 0x7C00 ) >> 7, ( pixel & 0x3E0 ) >> 2, ( pixel & 0x1F ) << 3 ) );
}
}
}

m_Fonts[ i ].Characters[ k ] = bmp;
}
}
}
}
}
}

<span style="color:blue public <span style="color:blue unsafe <span style="color:blue static Bitmap DrawText( <span style="color:blue int fontId, <span style="color:blue string text, <span style="color:blue short hueId )
{
ASCIIFont font = ASCIIFont.GetFixed( fontId );

Bitmap result =
<span style="color:blue new Bitmap( font.GetWidth( text ), font.Height );
BitmapData surface =
result.LockBits( <span style="color:blue new Rectangle( 0, 0, result.Width, result.Height ), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb );

<span style="color:blue int dx = 0;

<span style="color:blue for( <span style="color:blue int i = 0; i < text.Length; ++i )
{
Bitmap bmp =
font.GetBitmap( text[ i ] );
BitmapData chr =
bmp.LockBits( <span style="color:blue new Rectangle( 0, 0, bmp.Width, bmp.Height ), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb );

<span style="color:blue for( <span style="color:blue int dy = 0; dy < chr.Height; ++dy )
{
<span style="color:blue int* src =
( (<span style="color:blue int*)chr.Scan0 ) + ( chr.Stride * dy );
<span style="color:blue int* dest =
( ( (<span style="color:blue int*)surface.Scan0 ) + ( surface.Stride * ( dy + ( font.Height - chr.Height ) ) ) ) + ( dx << 2 );

<span style="color:blue for( <span style="color:blue int k = 0; k < chr.Width; ++k )
*dest++ = *src++;

}

dx += chr.Width;
bmp.UnlockBits( chr );
}

result.UnlockBits( surface );

hueId = (<span style="color:blue short)(( hueId & 0x3FFF ) - 1);
<span style="color:blue if( hueId >= 0 && hueId < Hues.List.Length )
{
Hue hueObject = Hues.List[ hueId ];

<span style="color:blue if( hueObject != <span style="color:blue null )
{
hueObject.ApplyTo( result, ( ( hueId & 0x8000 ) == 0 ) );
}
}

<span style="color:blue return result;
}
}
}

[/code]


When i try to debug or run i get an error {"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."}
Coming from this line:
<div style="color:black; background-color:white
<pre><span style="color:blue using (Bitmap bmp = Ultima.ASCIIText.DrawText(fontnumber, <span style="color:#a31515 entry, sHue))
[/code]


<br/>

View the full article
 
Back
Top