首页 新闻 会员 周边 捐助

求个在pictureBox1中的移动图片的编程方法、思想和代码啊

0
悬赏园豆:40 [已解决问题] 解决于 2012-07-27 09:40

最近在学习GDI  在网上找了段代码 就是 移动pictureBox1中的图片
但是在运行时老提示说; Bitmap bm = new Bitmap(this.pictureBox1.Image);中this.pictureBox1.Image 和  bm  的值为 null  求大侠解答。啊啊

 

 

 


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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            foreach (Control c in this.Controls)
            {
                if (c is PictureBox)
                {
                    c.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
                    c.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
                }

            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void button1_Click(object sender, EventArgs e)
        {
         /* Graphics g = this.CreateGraphics();//生成图形对象
            SolidBrush BlueBrush = new SolidBrush(Color.Blue);//生成填充用的画刷
            int x = 15;//定义外接矩形的左上角坐标和高度及宽度
            int y = 15;
            int width = 200;
            int height = 100;
            Rectangle rect = new Rectangle(x, y, width, height);//定义矩形
            g.FillRectangle(BlueBrush, rect);//填充矩形*/

           OpenFileDialog ofd = new OpenFileDialog();//实例化文件打开对话框
            ofd.Filter = "JPG|*.jpg|Bmp|*.bmp|所有文件|*.*";//设置对话框打开文件的括展名
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                Bitmap bmpformfile = new Bitmap(ofd.FileName);                            //获取打开的文件
                Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);   //新建画布
                //Graphics g = Graphics.FromImage(bmp);                             //新建画布
                 Graphics g = pictureBox1.CreateGraphics();


                g.DrawImage(bmpformfile, 0, 0);
               // g = pictureBox1.CreateGraphics();
                g.DrawImage(bmp, 0, 0);
                ofd.Dispose();


            }
        }

        bool wselected = false;
        Point p = new Point();

 

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            pictureBox1.Cursor = Cursors.Hand; //按下鼠标时,将鼠标形状改为手型
            wselected = true;
            p.X = e.X;
            p.Y = e.Y;

        }

 

        int driftX = 0, driftY = 0;
        int mx = 0, my = 0;

 

 

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (wselected)
            {
                driftX = p.X - e.X;
                driftY = p.Y - e.Y;

                mx = mx - driftX;
                my = my - driftY;

                Bitmap bm = new Bitmap(this.pictureBox1.Image);
                Graphics g = pictureBox1.CreateGraphics();
                g.Clear(pictureBox1.BackColor);
                g.DrawImage(bm, mx, my);

                p.X = e.X;
                p.Y = e.Y;

                bm.Dispose();
                g.Dispose();//图像移动的距离
            }

        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
         
            pictureBox1.Cursor = Cursors.Default; //松开鼠标时,形状恢复为箭头
            wselected = false ;
            this.pictureBox1.Cursor = Cursors.Default;
        }
      
    }

C#
小五子的主页 小五子 | 初学一级 | 园豆:123
提问于:2012-06-08 13:56
< >
分享
最佳答案
0
/// <summary>
/// method to rotate an image either clockwise or counter-clockwise
/// </summary>
/// <param name="img">the image to be rotated</param>
/// <param name="rotationAngle">the angle (in degrees).
/// NOTE: 
/// Positive values will rotate clockwise
/// negative values will rotate counter-clockwise
/// </param>
/// <returns></returns>
public static Image RotateImage(Image img, float rotationAngle)
{
    //create an empty Bitmap image
    Bitmap bmp = new Bitmap(img.Width, img.Height);

    //turn the Bitmap into a Graphics object
    Graphics gfx = Graphics.FromImage(bmp);

    //now we set the rotation point to the center of our image
    gfx.TranslateTransform((float)bmp.Width / 2, (float)bmp.Height / 2);

    //now rotate the image
    gfx.RotateTransform(rotationAngle);

    gfx.TranslateTransform(-(float)bmp.Width / 2, -(float)bmp.Height / 2);

    //set the InterpolationMode to HighQualityBicubic so to ensure a high
    //quality image once it is transformed to the specified size
    gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;

    //now draw our new image onto the graphics object
    gfx.DrawImage(img, new Point(0, 0));

    //dispose of our Graphics object
    gfx.Dispose();

    //return the image
    return bmp;
}
收获园豆:40
悟行 | 专家六级 |园豆:12559 | 2012-06-08 15:20
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册