100% found this document useful (1 vote)
44 views

Using Using Using Using Using Using Using Using Namespace Public Partial Class

This C# code demonstrates persisting graphics by using a bitmap in memory. When the form loads, it creates a bitmap with the same dimensions as the form's client area. This bitmap is used as the drawing surface. A Graphics object is used to draw to the bitmap. When the form paints, it draws the bitmap to the form. Buttons allow adding text to the bitmap by drawing strings to the Graphics object referencing the bitmap. The bitmap is disposed of when the form closes to free resources.

Uploaded by

Simon
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
44 views

Using Using Using Using Using Using Using Using Namespace Public Partial Class

This C# code demonstrates persisting graphics by using a bitmap in memory. When the form loads, it creates a bitmap with the same dimensions as the form's client area. This bitmap is used as the drawing surface. A Graphics object is used to draw to the bitmap. When the form paints, it draws the bitmap to the form. Buttons allow adding text to the bitmap by drawing strings to the Graphics object referencing the bitmap. The bitmap is disposed of when the form closes to free resources.

Uploaded by

Simon
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 3

// Demonstrates using a bitmap in memory to create

persisting Graphics.
//

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Persisting_Graphics
{
public partial class fclsMain : Form
{
// Bitmap object to draw to.
private Bitmap m_objDrawingSurface;

public fclsMain()
{
InitializeComponent();
}

private void fclsMain_Load(object sender, EventArgs


e)
{
// Create a drawing surface with the same
dimensions
// as the client area of the form.
m_objDrawingSurface = new
Bitmap(this.ClientRectangle.Width,
this.ClientRectangle.Height,
System.Drawing.Imaging.PixelFormat.Format24
bppRgb);
InitializeSurface();
}

private void InitializeSurface()


{
Graphics objGraphics;

// Create a Graphics object that references the


bitmap and clear it.
objGraphics =
Graphics.FromImage(m_objDrawingSurface);
objGraphics.Clear(SystemColors.Control);

// Free up resources
objGraphics.Dispose();
}

private void fclsMain_Paint(object sender,


PaintEventArgs e)
{
Graphics objGraphics;

// you cant modify e.Graphics directly


objGraphics = e.Graphics;

// Draw the contents of the bitmap on the form.


objGraphics.DrawImage(m_objDrawingSurface, 0, 0,
m_objDrawingSurface.Width,
m_objDrawingSurface.Height);

// Free resources
objGraphics.Dispose();
}

private void btnDrawText_Click(object sender,


EventArgs e)
{
Graphics objGraphics;
Font objFont;
int intFontSize, intTextX, intTextY;

Random RandomGenerator = new Random();

// if no input has been entered, get out.


if (txtInput.Text == "")
return;

// Create a Graphics object using the memory


bitmap.
objGraphics =
Graphics.FromImage(m_objDrawingSurface);

// Create a random number for the font size,


keep between 8 & 48.
intFontSize = RandomGenerator.Next(8, 48);
// random number for text X coord.
intTextX = RandomGenerator.Next(0,
this.ClientRectangle.Width);
// random number for text Y coord.
intTextY = RandomGenerator.Next(0,
this.ClientRectangle.Height);

// Create a new font object


objFont = new Font("Ariel", intFontSize,
FontStyle.Bold);

// Draw the users text


objGraphics.DrawString(txtInput.Text, objFont,
Brushes.Red, intTextX, intTextY);

// Clean up
objGraphics.Dispose();

// force the form to paint it self.


this.Invalidate();
}

private void fclsMain_FormClosed(object sender,


FormClosedEventArgs e)
{
// Free up global object on form closed
m_objDrawingSurface.Dispose();
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy