c#中如何实现图片从四角进入啊?我不知道那个for循环怎么写...

解决方案 »

  1.   

    Draw有2个参数,x,y表示左上角位置,你改变一下就行了。
    比如左上角进入,就是x,y不停增大
    如果你不会绘图。就设置一个PictureBox控件,修改Left,Top属性。
      

  2.   

    我设置了,但是运行结果总是从左侧显示,不是左上角,你看看我的代码吧:
    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 );
     
                    }
            }
      

  3.   

    x,y同时改变…
    给你写个范例,左上角进入,右下角出去的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控件的重绘功能还原,功能演示结束。
      

  4.   

    我发给你看一下吧!
    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);
                }
            }
        }
    }