c#中如何实现图片从四角进入啊?我不知道那个for循环怎么写...
解决方案 »
- vs.net 2010 通过oledb连接 oracle数据库问题请教
- vs2008组件
- 为什么绑定了listview1的tag值,但是listview1 的Tag为Null
- Winform 弹出选择文件对话窗口,线程错误!!!
- 请问高手,为什么js对只读的控件操作的结果反应不到后台呢
- 大家帮下忙:C#如何向指定窗口发键盘消息...
- 问一个跟xsd有关的问题
- SQL SERVER中我使用了框架,C#里面我想自己写 MODEL
- 怎样用A网页替代B网页?(关于网页模板)急急!!
- 每隔1秒触发一次事件怎样确定?
- 找不到类型或命名空间名称“BLL”(是否缺少using指令或程序引用?)
- c# 怎么关闭ShowDialog打开的窗体?
比如左上角进入,就是x,y不停增大
如果你不会绘图。就设置一个PictureBox控件,修改Left,Top属性。
private void 左上角ToolStripMenuItem_Click(object sender, EventArgs e)
{
int x, y;
Graphics g = pictureBox1.CreateGraphics();
g.Clear(this .BackColor );
box = new Bitmap(pictureBox1 .Image );
width = this.pictureBox1.Image.Width;
height = pictureBox1.Image.Height;
for (x = 0; x < Width; x++)
{
g.DrawImage(box, 0, 0, x, height );
}
for (y = 0; y < Height; y++)
{
g.DrawImage(box, 0, 0, y , width );
}
}
给你写个范例,左上角进入,右下角出去的Image image = pictureBox1.Image;//记录下当前的位图
//保存当前的图片,因为下面的行为将不会触发区域无效,控件不会主动将Image属性中的图片绘制到屏幕上
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
//创建Graphics对象,用于对bmp缓冲图片绘图
using (Graphics g = Graphics.FromImage(bmp))
{
//创建Graphics对象,用于对PictureBox贴图
using (Graphics gp = pictureBox1.CreateGraphics())
{
//记录一个值,表示width和height乘以-1的最小值,以便最初的图片在屏幕以外
int i = -Math.Max(pictureBox1.Width, pictureBox1.Height);
//i表示循环变量
while (i < pictureBox1.Width || i < pictureBox1.Height)
{
//清理缓存图片中原有数据
g.Clear(this.BackColor);
//绘制PictureBox中设置的Image到缓存图上,位置每次往右下角对角线方向偏移一个像素,
g.DrawImage(image, i, i);
//将缓存图片更新到PictureBox控件上,目的是减少对PictureBox控件的更新次数,避免闪烁
gp.DrawImage(bmp, 0, 0);
i++;//自增变量
}
}
}
pictureBox1.Refresh();//完成后调用PictureBox控件的重绘功能还原,功能演示结束。
C#code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int width, height;
Bitmap box;
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "bmp文件(*.bmp)|*.bmp|JPG文件(*.jpg)|*.jpg|GIF文件(*.gif)|*.gif";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
Bitmap box = new Bitmap(openFileDialog1.FileName);
pictureBox1.Image = box;
}
} private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Title = "保存图片";
dlg.OverwritePrompt = true;
dlg.InitialDirectory = "D:\\";
dlg.Filter = "bmp文件(*.bmp)|*.bmp|JPG文件(*.jpg)|*.jpg|GIF文件(*.gif)|*.gif";
dlg.FilterIndex = 1;
if (dlg.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image.Save(dlg.FileName);
}
} private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
} private void 左上角ToolStripMenuItem_Click(object sender, EventArgs e)
{
Image image = pictureBox1.Image;
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
using (Graphics gp = pictureBox1.CreateGraphics())
{
int i = -Math.Max(pictureBox1.Width, pictureBox1.Height);
while (i < pictureBox1.Width || i < pictureBox1.Height)
{
g.Clear(this.BackColor);
g.DrawImage(image, i, i);
gp.DrawImage(bmp, 0, 0);
i++;
}
}
}
pictureBox1.Refresh();
} private void 上到下ToolStripMenuItem_Click(object sender, EventArgs e)
{
int y;
Graphics g = pictureBox1.CreateGraphics();
g.Clear(this.BackColor);
box = new Bitmap(pictureBox1.Image);
width = pictureBox1.Image.Width;
height = pictureBox1.Image.Height;
for (y = 0; y < height; y++)
{
g.DrawImage(box, 0, 0, width, y);
}
} private void 左到右ToolStripMenuItem_Click(object sender, EventArgs e)
{
int x;
Graphics g = pictureBox1.CreateGraphics();
g.Clear(this.BackColor);
box = new Bitmap(pictureBox1.Image);
width = this.pictureBox1.Image.Width;
height = pictureBox1.Image.Height;
for (x = 0; x < width; x++)
{
g.DrawImage(box, 0, 0, x, height);
}
} private void 右上角ToolStripMenuItem_Click(object sender, EventArgs e)
{ // 从右上角飞到左下角 -----修改后
width = pictureBox1.Image.Width;
height = pictureBox1.Image.Height;
Graphics g2 = pictureBox2.CreateGraphics();
Image image1 = pictureBox1.Image; int width2=this.pictureBox2.Width ;
int height2=this.pictureBox2.Height ;
for (int x = width2 - image1.Width, y = 0; x >= -image1.Width && y <= height2; x--, y++)
{
g2.Clear(this.BackColor);
g2.DrawImage(image1, x,y);
System.Threading.Thread.Sleep(20);
}
//int x, y;
//Graphics g = pictureBox2.CreateGraphics();
//g.Clear(this.BackColor);
//box = new Bitmap(pictureBox1.Image);
//width = pictureBox1.Image.Width;
//height = pictureBox1.Image.Height;
//for (x = 0; x < width; x++)
//{
// y = height * x / width;
// Rectangle DestRect = new Rectangle(width - x, 0, x, y);
// Rectangle SrcRect = new Rectangle(0, height - y, width, height);
// g.DrawImage(box, DestRect, SrcRect, GraphicsUnit.Pixel);
// System.Threading.Thread.Sleep(10);
//}
} private void 左下角ToolStripMenuItem_Click(object sender, EventArgs e)
{
int x, y;
Graphics g = pictureBox2.CreateGraphics();
g.Clear(this.BackColor);
box = new Bitmap(pictureBox1.Image);
width = pictureBox1.Image.Width;
height = pictureBox1.Image.Height;
for (y = 0; y < height; y++)
{
x = width * y / height;
Rectangle DestRect = new Rectangle(0, height - y, x, y);
Rectangle SrcRect = new Rectangle(0, 0, width, height);
g.DrawImage(box, DestRect, SrcRect, GraphicsUnit.Pixel);
System.Threading.Thread.Sleep(10);
}
} private void 右下角ToolStripMenuItem_Click(object sender, EventArgs e)
{
box = new Bitmap(pictureBox1.Image);
Graphics g = pictureBox2.CreateGraphics();
g.Clear(this.BackColor);
width = pictureBox1.Image.Width;
height = pictureBox1.Image.Height;
int x, y;
for (x = 0; x < width; x++)
{
y = height * x / width;
Rectangle DestRect = new Rectangle(width - x, height - y, x, y);
Rectangle SrcRect = new Rectangle(0, 0, width, height);
g.DrawImage(box, DestRect, SrcRect, GraphicsUnit.Pixel);
System.Threading.Thread.Sleep(10);
} } private void 四周向中心ToolStripMenuItem_Click(object sender, EventArgs e)
{
box = new Bitmap(pictureBox1.Image);
width = this.pictureBox1.Image.Width;
height = this.pictureBox1.Image.Height;
Graphics g = this.pictureBox2.CreateGraphics();
g.Clear(this.BackColor);
Bitmap bmp = new Bitmap(width, height);
int x;
for (x = 0; x < width; x++)
{
for (int i = 0; i < height / 2; i++)
{
bmp.SetPixel(x, i, box.GetPixel(x, i));
bmp.SetPixel(width - x - 1, i, box.GetPixel(width - x - 1, i));
}
for (int i = 0; i < height / 2; i++)
{
bmp.SetPixel(x, height - 1 - i, box.GetPixel(x, height - 1 - i));
bmp.SetPixel(width - x - 1, height - i, box.GetPixel(width - x - 1, height- i-1));
}
g.DrawImage(bmp, 0, 0);
}
} private void 中心向四周ToolStripMenuItem_Click(object sender, EventArgs e)
{
box = new Bitmap(pictureBox1.Image);
Graphics g = pictureBox2.CreateGraphics();
g.Clear(this.BackColor);
width = pictureBox1.Image.Width;
height = pictureBox1.Image.Height;
for (int x = 0; x <= width / 2; x++)
{
int y = height * x / width;
Rectangle DestRect = new Rectangle(width / 2 - x, height / 2 - y, 2 * x, 2 * y);
Rectangle SrcRect = new Rectangle(0, 0, width, height);
g.DrawImage(box, DestRect, SrcRect, GraphicsUnit.Pixel);
System.Threading.Thread.Sleep(10);
}
}
}
}