|
|
|
Select your preferred language |
Listing 1 code.
using
System;
using
System.Drawing;
using
System.Drawing.Imaging;
using
System.Drawing.Drawing2D;
using
System.Collections;
using
System.ComponentModel;
using
System.Windows.Forms;
using
System.Data;
namespace
simpleblendcs
{
///
<summary>
/// Summary
description for Form1.
///
</summary>
public class
Form1 : System.Windows.Forms.Form
{
///
<summary>
///
Required designer variable.
///
</summary>
private
System.ComponentModel.Container components = null;
public
Form1()
{
//
//
Required for Windows Form Designer support
//
InitializeComponent();
//
//
TODO: Add any constructor code after InitializeComponent call
//
}
///
<summary>
///
Clean up any resources being used.
///
</summary>
protected
override void
Dispose( bool disposing )
{
if(
disposing )
{
if
(components != null)
{
components.Dispose();
}
}
base.Dispose(
disposing );
}
#region
Windows Form Designer generated code
///
<summary>
///
Required method for Designer support - do not modify
///
the contents of this method with the code editor.
///
</summary>
private
void InitializeComponent()
{
//
//
Form1
//
this.AutoScaleBaseSize
= new System.Drawing.Size(5, 13);
this.ClientSize
= new System.Drawing.Size(292, 266);
this.Name
= "Form1";
this.Text
= "Form1";
this.Load
+= new System.EventHandler(this.Form1_Load);
this.Paint
+= new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
#endregion
///
<summary>
///
The main entry point for the application.
///
</summary>
[STAThread]
static
void Main()
{
Application.Run(new
Form1());
}
Bitmap bm;
private
void Form1_Load(object
sender, System.EventArgs e)
{
bm=(Bitmap)Image.FromFile("..\\..\\sunset.jpg");
this.ClientSize=new
Size(bm.Width*2,bm.Height);
}
private
void Form1_Paint(object
sender, System.Windows.Forms.PaintEventArgs e)
{
if(bm==null)
return;
//Draw
an image with some alphablended objects on.
e.Graphics.DrawImageUnscaled(bm,0,0);
for(int
y=0;y<8;y++)
{
for(int
x=0;x<8;x++)
{
Color c=Color.FromArgb(32*y+(4*x),Color.Black);
SolidBrush
sb=new SolidBrush(c);
e.Graphics.FillEllipse(sb,bm.Width/8*x,bm.Height/8*y,bm.Width/8,bm.Height/8);
sb.Dispose();
}
}
//now
draw some solid objects with transparent images on them
GraphicsPath
gp=new GraphicsPath();
gp.AddRectangle(new
Rectangle(0,0,bm.Width/8,bm.Height/8));
gp.AddEllipse(bm.Width/80,bm.Height/80,(bm.Width/8)-(bm.Width/40),(bm.Height/8)-(bm.Width/40));
for(int
y=0;y<8;y++)
{
for(int
x=0;x<8;x++)
{
ImageAttributes
ia=new ImageAttributes();
ColorMatrix
cm=new ColorMatrix();
cm.Matrix33=1.0f/255*(32f*y+(4f*x));
ia.SetColorMatrix(cm);
Matrix mx=new Matrix(1,0,0,1,x*bm.Width/8+bm.Width,y*bm.Height/8);
e.Graphics.Transform=mx;
SolidBrush
sb=new SolidBrush(Color.Black);
e.Graphics.FillPath(sb,gp);
sb.Dispose();
e.Graphics.Transform=new
Matrix();
e.Graphics.DrawImage(bm,
new
Rectangle(bm.Width+x*(bm.Width/8),
y*(bm.Height/8),
bm.Width/8,
bm.Height/8),
x*(bm.Width/8),
y*(bm.Height/8),
bm.Width/8,
bm.Height/8,GraphicsUnit.Pixel,
ia);
ia.Dispose();
}
}
}
}
}
Imports
System.Drawing.Drawing2D
Imports
System.Drawing.Imaging
Public
Class Form1
Inherits System.Windows.Forms.Form
Private bm As
Bitmap
#Region
" Windows Form Designer generated code "
Public Sub
New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent()
call
End Sub
'Form overrides dispose to clean up the component
list.
Protected Overloads
Overrides Sub
Dispose(ByVal disposing
As Boolean)
If disposing Then
If Not
(components Is Nothing)
Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As
System.ComponentModel.IContainer
'NOTE: The following procedure is required by the
Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()
'
'Form1
'
Me.AutoScaleBaseSize =
New System.Drawing.Size(5, 13)
Me.ClientSize = New
System.Drawing.Size(448, 294)
Me.Name = "Form1"
Me.Text = "Form1"
End Sub
#End
Region
Private Sub
Form1_Load(ByVal sender
As Object, ByVal
e As System.EventArgs)
Handles MyBase.Load
bm =
CType(Image.FromFile("..\..\sunset.jpg"),
Bitmap)
Me.ClientSize = New
Size(bm.Width * 2, bm.Height)
End Sub
Private Sub
Form1_Paint(ByVal sender
As Object,
ByVal e As
System.Windows.Forms.PaintEventArgs) Handles
MyBase.Paint
If bm Is
Nothing Then
Return
End If
'Draw an image with some alphablended objects on.
e.Graphics.DrawImageUnscaled(bm, 0, 0)
Dim x As
Integer
Dim y As
Integer
For y = 0 To 7
For x = 0 To 7
Dim c
As Color = Color.FromArgb(32 * y + (4 * x), Color.Black)
Dim sb
As New SolidBrush(c)
e.Graphics.FillEllipse(sb, CInt(bm.Width
* x / 8), CInt(bm.Height * y / 8),
CInt(bm.Width / 8),
CInt(bm.Height / 8))
sb.Dispose()
Next
Next
'now draw some solid objects with transparent images
on them
Dim gp As
New GraphicsPath
gp.AddRectangle(New Rectangle(0, 0, bm.Width / 8, bm.Height / 8))
gp.AddEllipse(CInt(bm.Width / 80),
CInt(bm.Height / 80),
CInt((bm.Width / 8) - (bm.Width / 40)), CInt((bm.Height
/ 8) - (bm.Width / 40)))
For y = 0 To 7
For x = 0 To 7
Dim ia
As New ImageAttributes
Dim cm
As New ColorMatrix
cm.Matrix33 = 1.0F / 255 * (32.0F * y + (4.0F * x))
ia.SetColorMatrix(cm)
Dim mx
As New Matrix(1, 0, 0, 1, x * bm.Width /
8 + bm.Width, y * bm.Height / 8)
e.Graphics.Transform = mx
Dim sb
As New SolidBrush(Color.Black)
e.Graphics.FillPath(sb, gp)
sb.Dispose()
e.Graphics.Transform = New
Matrix
e.Graphics.DrawImage(bm, _
New Rectangle(bm.Width + x * (bm.Width
/ 8), _
y * (bm.Height / 8), _
bm.Width / 8, _
bm.Height / 8), _
CInt(x * (bm.Width / 8)), _
CInt(y * (bm.Height / 8)), _
CInt(bm.Width / 8), _
CInt(bm.Height / 8),
GraphicsUnit.Pixel, _
ia)
ia.Dispose()
Next
Next
End Sub
End
Class
Press your browser
Back button to return to the article.