I am using custom font and if I want to change label's text it causes: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt" How can I fix that ?
private void LoadFont()
{
PrivateFontCollection pfc = new PrivateFontCollection();
int fontLength = Resources.Nexa_Regular.Length;
byte[] fontdata = Resources.Nexa_Regular;
System.IntPtr data = Marshal.AllocCoTaskMem(fontLength);
Marshal.Copy(fontdata, 0, data, fontLength);
pfc.AddMemoryFont(data, fontLength);
lblStatus.Font = new Font(pfc.Families[0], lblStatus.Font.Size);
Marshal.FreeCoTaskMem(data);
pfc.Dispose();
}
private void MainForm_Load(object sender, EventArgs e)
{
LoadFont();
}
private void SetStatus()
{
if (IsActive())
{
lblStatus.Text = "Active"; // Error: Attempted to read or write protected memory. This is often an indication that other memory is corrupt
lblStatus.ForeColor = Color.Green;
}
else
{
lblStatus.Text = "Not Active";
lblStatus.ForeColor = Color.Red;
}
}