1.图片缩放
using System;
using System.Windows.Forms;
using System.Drawing;
class haha : Form
{
static void Main()
{
Application.Run(new haha());
}
Bitmap bit;
int x, y;
haha()
{
ClientSize = new Size(400, 400);
bit = new Bitmap(800, 800);
Graphics.FromImage(bit).DrawImage(Image.FromFile("beauty.jpg"),new Rectangle(new Point(0,0),new Size(bit.Width,bit.Height)));
KeyDown += roll;
MouseWheel += zoom;
x = y = 0;
}
void zoom(object o, MouseEventArgs e)
{
if ((bit.Width < 100||bit.Height<100)&&e.Delta<0) return;
if (bit.Width + e.Delta < 0 || bit.Height + e.Delta < 0) return;
Bitmap b = new Bitmap(bit.Width + e.Delta, bit.Height + e.Delta);
Graphics.FromImage(b).DrawImage(bit, new Rectangle(0, 0, b.Width, b.Height));
bit = b;
draw();
}
void draw()
{
if (x >= bit.Width - ClientSize.Width) x = bit.Width - 1 - ClientSize.Width;
if (y >= bit.Height - ClientSize.Height) y = bit.Height - ClientSize.Height - 1;
if (x < 0) x = 0;
if (y < 0) y = 0;
Bitmap b = new Bitmap(ClientSize.Width, ClientSize.Height);
int i, j;
for (i = 0; i < ClientSize.Width; i++)
for (j = 0; j < ClientSize.Height; j++)
{
if (x + i >= bit.Width || y + j >= bit.Height)
b.SetPixel(i, j, Color.AliceBlue);
else
b.SetPixel(i, j, bit.GetPixel(x + i, y + j));
}
CreateGraphics().DrawImage(b, 0, 0);
}
void roll(object o, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up: y -= 10; break;
case Keys.Down: y += 10; break;
case Keys.Right: x += 10; break;
case Keys.Left: x -= 10; break;
}
draw();
}
}
2.位图操作-乱画
using System.Drawing;
using System.Windows.Forms;
using System;
class haha:Form
{
Bitmap bit;
Random r = new Random();
static void Main()
{
Application.Run(new haha());
}
void circle(int xx, int yy, int r,Color c)
{
int x; double y;
for (x = xx - r; x < xx + r; x++)
{
y = Math.Sqrt(r * r - (xx - x) * (xx - x));
if (x < 0 || yy - y < 0 || x >= bit.Width || yy + y >= bit.Height) continue;
bit.SetPixel(x, (int)(y+yy), c);
bit.SetPixel(x, (int)( yy-y), c);
}
}
void line(int x1,int y1,int x2,int y2,Color c)
{
int fx, fy, tx, ty;
if(x1==x2){
if(y1>y2){
fx=x2;fy=y2;
tx=x1;ty=y1;
}
else {
fx=x1;fy=y1;
tx=x2;ty=y2;
}
int i;
for (i = fy; i < ty; i++)
{
bit.SetPixel(x1, i, c);
}
CreateGraphics().DrawImage(bit,0,0);
return;
}
if (x1 > x2)
{
fx = x2; fy = y2;
tx = x1; ty = y1;
}
else
{
fx = x1; fy = y1;
tx = x2; ty = y2;
}
double k = (ty - fy) / (tx - fx);
double y;
int x;
for (x = fx; x < tx; x++)
{
y = k * x + ((double)y1 - k * x1);
bit.SetPixel(x, (int)y, c);
}
CreateGraphics().DrawImage(bit, 0, 0);
}
haha()
{
ClientSize = new Size(500, 500);
bit = new Bitmap(ClientSize.Width, ClientSize.Height);
Graphics.FromImage(bit).Clear(Color.White);
Timer t1 = new Timer();
t1.Interval = 5000;
t1.Tick += draw_line;
//t1.Start();
Timer t2 = new Timer();
t2.Interval = 10;
t2.Tick += draw_circle;
t2.Start();
}
void draw_line(object o,EventArgs e)
{
line(r.Next()%ClientSize.Width, r.Next()%ClientSize.Height, r.Next()%ClientSize.Width, r.Next()%ClientSize.Height, Color.FromArgb(r.Next()));
}
void draw_circle(object o, EventArgs e)
{
int rr=r.Next() % 100 + 1;
int xx=r.Next() % ClientSize.Width;
int yy=r.Next() % ClientSize.Height;
for (int i = 0; i < 16&&rr>i;i++ )
circle(xx,yy , rr-i, Color.FromArgb(r.Next()));
CreateGraphics().DrawImage(bit, 0, 0);
}
}
3.图片旋转
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
KeyDown += draw;
bitmap = new Bitmap(Image.FromFile("0.jpg"));
ClientSize = new Size(700,700);
}
Bitmap rotate(Bitmap source)
{
Bitmap bit = new Bitmap(source.Height, source.Width);
for (int x = 0; x < bit.Width; x++)
{
for (int y = 0; y < bit.Height; y++)
{
bit.SetPixel(x, y, source.GetPixel(bit.Height- y-1, x));
}
}
return bit;
}
Bitmap bitmap;
void draw(object o, EventArgs e)
{
CreateGraphics().Clear(Color.Black);
bitmap = rotate(bitmap);
CreateGraphics().DrawImage(bitmap, 0, 0);
}
}
4.鼠标单击拖动图片
using System;
using System.Windows.Forms;
using System.Drawing;
class window : Form
{
Rectangle rec=new Rectangle(23,23,23,23);
Bitmap b=new Bitmap("beauty.jpg");
int x, y;
int dx, dy;
bool move;
public window(){
MouseClick +=click;
MouseMove += OnMove;
Paint += paint;
Size = new Size(800,800);
x = y = 0;
}
void OnMove(object o, MouseEventArgs e)
{
if (move)
{
Bitmap bit = new Bitmap(Width,Height);
Graphics g = Graphics.FromImage(bit);
g.Clear(Color.AliceBlue);
g.DrawImage(b, e.X+dx, e.Y+dy);
for (int i = 10; i < bit.Width; i++)
bit.SetPixel(i, 100, Color.Red);
CreateGraphics().DrawImage(bit, 0, 0);
}
}
void paint(object o, EventArgs e)
{
CreateGraphics().DrawImage(b,0,0);
}
void click(object o, MouseEventArgs e)
{
if (e.X > x && e.X < x + b.Width && e.Y > y && e.Y < y + b.Height && move == false) { move = true; dx = x - e.X; dy = y - e.Y; }
else if (move == true)
{
x = e.X+dx; y = e.Y+dy;
move = false;
}
}
public static void Main()
{
Application.Run(new window());
}
}
5.没有边框的窗口
public partial class Form1 : Form
{
public Form1()
{
ClientSize = Screen.PrimaryScreen.Bounds.Size;
FormBorderStyle = FormBorderStyle.None;
Paint += draw;
TransparencyKey = DefaultBackColor;
ShowInTaskbar = false;
}
void draw(object o, EventArgs e)
{
CreateGraphics().Clear(DefaultBackColor);
Bitmap bit=new Bitmap(ClientSize.Width,ClientSize.Height);
bit.MakeTransparent(Color.White);
Graphics.FromImage(bit).DrawString("东大微雕\n文韬武略\n天下第一", new Font(new FontFamily("Consolas"),200,GraphicsUnit.Pixel),new SolidBrush(Color.Red),new Rectangle(30,30,ClientRectangle.Right,ClientRectangle.Bottom));
//BackgroundImage = bit;
//释放上面这句就会导致闪烁
CreateGraphics().DrawImage(bit,ClientRectangle);
}
}
6.全屏的窗口
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Size = MaximumSize;
WindowState = FormWindowState.Maximized;
FormBorderStyle = FormBorderStyle.None;
Paint += paint;
files= Directory.GetFiles(Directory.GetCurrentDirectory(),"*.jpg");
KeyDown += down;
}
void down(object o, KeyEventArgs e)
{
index++;
Invalidate();
}
string[] files;
int index=0;
void paint(object o, PaintEventArgs e)
{
Bitmap bit = new Bitmap(ClientSize.Width,ClientSize.Height);
Graphics.FromImage(bit).DrawImage(Image.FromFile(files[index%files.Length]), ClientRectangle);
e.Graphics.DrawImage(bit,0,0);
}
}
7.分工演示器
public partial class Form1 : Form
{
SpeechSynthesizer teller = new SpeechSynthesizer();
void tell(string s)
{
teller.Speak(s);
}
void crossLine(int from, int to)
{
int w = bit.Width / (size + 1);
int h = bit.Height / (size + 1);
Graphics g = Graphics.FromImage(bit);
Pen p = new Pen(new SolidBrush(Color.Gold),4);
g.DrawLine(p, work[from] * w + w + w / 2, from * h + h + h / 2, work[to] * w + w + w / 2, to * h + h + h / 2);
g.DrawLine(p, work[to] * w + w + w / 2, from * h + h + h / 2, work[from] * w + w + w / 2, to * h + h + h / 2);
CreateGraphics().DrawImage(bit,0,0);
}
void go()
{
int i, j;
bool changed = true;
while (changed)
{
tell("只要有变化,那就接着调整.调整哇调整,一直到没法再调整了");
changed = false;
for (i = 0; i < size; i++)
{
for (j = size - 1; j >= 0; j--)
{
if (a[i, work[j]] + a[j, work[i]] < a[i, work[i]] + a[j, work[j]])
{
crossLine(i, j);
tell("工人" + man[i] + "干任务" + (char)(work[i] + 'A') + "需要" + a[i, work[i]] + "的时间");
tell("工人" + man[j] + "干任务" + (char)(work[j] + 'A') + "需要" + a[j, work[j]] + "的时间");
tell("他们交换一下任务,岂不是更快");
int temp = work[i];
work[i] = work[j];
work[j] = temp;
draw();
changed = true;
}
}
}
}
}
int[] work;
public Form1()
{
InitializeComponent();
WindowState = FormWindowState.Maximized;
Text = "分工演示器-written for jiege,made by weidiao";
var set = teller.GetInstalledVoices();
teller.SelectVoice(set[0].VoiceInfo.Name);
init();
Paint += delegate
{
draw();
go();
tell("杰哥,你可以百度一下模拟淬火算法,这种方法适用于解决NP问题,但它的缺点就是最后所求的结果不一定对");
tell("这种方法能够保证得到局部最优解,至于有多么接近最优解,这取决于你所选择的初始值");
tell("于是,我们可以大量的用很多初始值进行迭代优化,从中选取最好的");
};
Resize += delegate
{
bit = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
draw();
};
}
Bitmap bit;
char[] man = "甲乙丙丁戊己庚辛壬癸".ToCharArray();
void draw()
{
Graphics g = Graphics.FromImage(bit);
g.Clear(Color.Black);
int i, j;
int w = bit.Width / (size + 1);
int h = bit.Height / (size + 1);
g.FillRectangle(new SolidBrush(Color.Gray), new Rectangle(0, 0, w - 2, h - 2));
g.DrawLine(new Pen(new SolidBrush(Color.Black), 2), 0, 0, w, h);
g.DrawString("任务", new Font("楷体", 20, FontStyle.Bold), new SolidBrush(Color.Crimson), new RectangleF(w / 2, 0, w / 2, h / 2));
g.DrawString("人物", new Font("楷体", 20, FontStyle.Bold), new SolidBrush(Color.DarkOrange), new RectangleF(0, h / 2, w / 2, h / 2));
for (i = 0; i < size; i++)
{
RectangleF rec = new RectangleF(w + w * i, 0, w - 2, h - 2);
g.FillRectangle(new SolidBrush(Color.Gray), rec);
g.DrawString((char)(i + 'A') + "", new Font("Consolas", 40, FontStyle.Bold), new SolidBrush(Color.Crimson), rec);
}
for (i = 0; i < size; i++)
{
RectangleF rec = new RectangleF(0, h + i * h, w - 2, h - 2);
g.FillRectangle(new SolidBrush(Color.Gray), rec);
g.DrawString(man[i] + "", new Font("Consolas", 40, FontStyle.Bold), new SolidBrush(Color.DarkOrange), rec);
}
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
RectangleF rec = new RectangleF(j * w + w, h + i * h, w - 2, h - 2);
Color back = work[i] == j ? Color.Gainsboro : Color.Gray;
g.FillRectangle(new SolidBrush(back), rec);
g.DrawString(a[i, j] + "", new Font("Consolas", 40, FontStyle.Bold), new SolidBrush(Color.Red), rec);
}
}
CreateGraphics().DrawImage(bit, 0, 0);
}
int[,] a;
Random r = new Random();
int size;
void init()
{
size = 4;
a = new int[size, size];
work = new int[size];
int i, j;
for (i = 0; i < size; i++) work[i] = i;
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
a[i, j] = r.Next() % 20 + 2;
}
}
}
}