标题:C# 图片旋转问题
取消只看楼主
kenshin5868
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-7-31
 问题点数:0 回复次数:2 
C# 图片旋转问题
我是通过listbox加载图片到picturebox中,但是加载过去的图片无法旋转。
旋转代码是这么写的
this.pictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);
this.pictureBox1.Refresh();
但是这个代码能够旋转我手动添加到picturebox中的图片


请高手指教哈,多谢多谢啦~~~
搜索更多相关主题的帖子: 图片 
2011-03-10 11:52
kenshin5868
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-7-31
得分:0 
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using
using System.Xml;
using System.Text.RegularExpressions;
using System.Drawing.Drawing2D;



namespace Pic
{
   
    public partial class Form1 : Form
    {
        //private System.Drawing.Bitmap modifiedBitmap;
        public Form1()
        {
            InitializeComponent();
            pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);
            //this.MouseWheel += new MouseEventHandler(Form1_MouseWheel);
        }

        TextBox textBox;
        float zoom;
        Point newPoint;      
        bool IsLoadBmp;   
        Bitmap bmp;
        bool DrawingFilmDbSelectview;
        System.Drawing.Point mouseOffset;
        MatchCollection arrays;


        private SourcePoint[] configPoint;
        private List<string> sourceData;
        private void Form1_Load(object sender, EventArgs e)
        {
            
            configPoint = new SourcePoint[] {
                                      new SourcePoint(new Point(73, 137),160, 36),
                                      new SourcePoint(new Point(62,194),269, 36),
                                      new SourcePoint(new Point(64, 1146),318, 27),
                                      new SourcePoint(new Point(369,123),269, 36),
                                      new SourcePoint(new Point(66, 379),445, 105),
                                      new SourcePoint(new Point(67, 319),650, 44),
                                      new SourcePoint(new Point(68, 382),650, 44),
                                      new SourcePoint(new Point(68, 662),461, 66),
                                      new SourcePoint(new Point(62, 734),665, 31),
                                      new SourcePoint(new Point(66, 782),641, 67),
                                      new SourcePoint(new Point(62, 867),467, 67),
                                      new SourcePoint(new Point(62, 533),665, 55),
                                      new SourcePoint(new Point(62, 1038),501, 82),
                                      new SourcePoint(new Point(62, 959),645, 40),
                                      new SourcePoint(new Point(62, 1264),387, 55),
                                      new SourcePoint(new Point(56, 1166),651, 89),
                                      new SourcePoint(new Point(65, 104),148, 403),
                                      new SourcePoint(new Point(698, 6),150, 533),
                                      new SourcePoint(new Point(281, 1481),295, 115),
                                      new SourcePoint(new Point(73, 180),160, 36)
                                   
                                  };
        }
        //按索引读取记事本内容
        private void ReadData(int index) {
            string path = @"D:\工作软件\MyTest.txt";
            if (File.Exists(path))
            {
                //装载记事本数据
                List<string> data = new List<string>();
                using (StreamReader reader = new StreamReader(path))
                {
                    string temp = null;
                    while ((temp = reader.ReadLine()) != null)
                    {
                        data.Add(temp);                                           //    讲独到的数据放入一个泛型集合里面
                    }
                }
                //重新组装记事本数据格式 <索引,值集合>
                IDictionary<int,List<string>> datas = new Dictionary<int,List<string>>();
                Regex regex = new Regex(@"<\d+>(.*?)<\d+>");                //用正则表达式匹配需要的内容
                for (int i = 0; i < data.Count; i++)
                {
                    arrays = regex.Matches(data[i]);
                    List<string> temp = new List<string>();
                    for (int j = 0; j < arrays.Count - 1; j++)
                    {
                        temp.Add(arrays[j].Groups[1].Value);
                    }
                    datas.Add(Convert.ToInt32(arrays[arrays.Count - 1].Groups[1].Value), temp);
                }
                if (datas.ContainsKey(index))
                {
                    sourceData = datas[index];
                    //注册事件
                    this.RegisterEvent(txtFirstName_Leave);
                }
                else {
                    this.RegisterEvent(txtFirstName_Leave_1);
                }
            }
            else {
                this.RegisterEvent(txtFirstName_Leave_1);
            }
            
        }

        private void RegisterEvent(EventHandler funcName){
            this.txtFirstName.Leave += new EventHandler(funcName);
            this.txtMiddleName.Leave += new EventHandler(funcName);
            this.txtLastName.Leave += new EventHandler(funcName);
            this.txtSuffix.Leave += new EventHandler(funcName);
            this.txtZCS.Leave += new EventHandler(funcName);
            this.txtaddress1.Leave += new EventHandler(funcName);
            this.txtaddress2.Leave += new EventHandler(funcName);
            this.txtMobilePhone.Leave += new EventHandler(funcName);
            this.txtProvider.Leave += new EventHandler(funcName);
            this.txtYBlank.Leave += new EventHandler(funcName);
            this.txtHomePhone.Leave += new EventHandler(funcName);
            this.txtMHblank.Leave += new EventHandler(funcName);
            this.txtEmail.Leave += new EventHandler(funcName);
            this.txtDateOfBirth.Leave += new EventHandler(funcName);
            this.txtGender.Leave += new EventHandler(funcName);
            this.txtSighDate.Leave += new EventHandler(funcName);
            this.txtCountry.Leave += new EventHandler(funcName);
            this.txtLanguage.Leave += new EventHandler(funcName);
            this.txtSignature.Leave += new EventHandler(funcName);
        }
        void txtFirstName_Leave(object sender, EventArgs e)
        {
            textBox = sender as TextBox;
            if (!sourceData[Convert.ToInt32(textBox.Tag)].Equals(textBox.Text))
            {

                textBox.BackColor = Color.Yellow;
                groupBox1.Visible = true;
                this.lbldiyici.Text = (sourceData[Convert.ToInt32(textBox.Tag)]);
                this.lbldierci.Text = textBox.Text;
            }
            else
            {
                textBox.BackColor = Color.White;
                this.groupBox1.Visible = false;
            }
        }
        ///---------------------
        private void txtBox_Enter(object sender, EventArgs e)
        {   
            pnlTips.Parent = pictureBox1;
            this.pnlTips.BackColor = Color.FromArgb(30, Color.Red);
            SourcePoint point = this.configPoint[Convert.ToInt32((sender as TextBox).Tag)];
            this.pnlTips.Location = point.Target;
            this.pnlTips.Width = point.Width;
            this.pnlTips.Height = point.Height;
            this.pnlMain.AutoScrollPosition = new Point(this.pnlMain.AutoScrollPosition.X, this.pnlTips.Location.Y - this.pnlMain.Height / 2);
            TextBox tb = sender as TextBox;
            tb.BackColor = Color.Aquamarine;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.ReadData(this.listBox1.SelectedIndex);
            string path = @"D:\工作软件\MyTest.txt";
            if (!File.Exists(path))
            {
                using (StreamWriter sw = File.CreateText(path))
                {
                    sw.WriteLine("<1>" + this.txtFirstName.Text.ToString() + "<1>" + "<2>" + this.txtMiddleName.Text.ToString() + "<2>"
                    + "<3>" + this.txtLastName.Text.ToString() + "<3>" + "<4>" + this.txtSuffix.Text.ToString() + "<4>" + "<5>" + this.txtZCS.Text.ToString() + "<5>" + "<6>" + this.txtaddress1.Text.ToString() + "<6>"
                    + "<7>" + this.txtaddress2.Text.ToString() + "<7>" + "<8>" + this.txtMobilePhone.Text.ToString() + "<8>" + "<9>" + this.txtProvider.Text.ToString() + "<9>"
                    + "<10>" + this.txtYBlank.Text.ToString() + "<10>" + "<11>" + this.txtHomePhone.Text.ToString() + "<11>" + "<12>" + this.txtMHblank.Text.ToString() + "<12>"
                    + "<13>" + this.txtEmail.Text.ToString() + "<13>" + "<14>" + this.txtDateOfBirth.Text.ToString() + "<14>" + "<15>" + this.txtGender.Text.ToString() + "<15>"
                    + "<16>" + this.txtSighDate.Text.ToString() + "<16>" + "<17>" + this.txtCountry.Text.ToString() + "<17>" + "<18>" + this.txtLanguage.Text.ToString() + "<18>"
                    + "<19>" + this.txtSignature.Text.ToString() + "<19><0>" + this.listBox1.SelectedIndex + "<0>");
                    sw.Close();
                }

            }
            else
            {
                using (StreamWriter sw = File.AppendText(path))
                {
                    sw.WriteLine("<1>" + this.txtFirstName.Text.ToString() + "<1>" + "<2>" + this.txtMiddleName.Text.ToString() + "<2>"
                        + "<3>" + this.txtLastName.Text.ToString() + "<3>" + "<4>" + this.txtSuffix.Text.ToString() + "<4>" + "<5>" + this.txtZCS.Text.ToString() + "<5>" + "<6>" + this.txtaddress1.Text.ToString() + "<6>"
                        + "<7>" + this.txtaddress2.Text.ToString() + "<7>" + "<8>" + this.txtMobilePhone.Text.ToString() + "<8>" + "<9>" + this.txtProvider.Text.ToString() + "<9>"
                        + "<10>" + this.txtYBlank.Text.ToString() + "<10>" + "<11>" + this.txtHomePhone.Text.ToString() + "<11>" + "<12>" + this.txtMHblank.Text.ToString() + "<12>"
                        + "<13>" + this.txtEmail.Text.ToString() + "<13>" + "<14>" + this.txtDateOfBirth.Text.ToString() + "<14>" + "<15>" + this.txtGender.Text.ToString() + "<15>"
                        + "<16>" + this.txtSighDate.Text.ToString() + "<16>" + "<17>" + this.txtCountry.Text.ToString() + "<17>" + "<18>" + this.txtLanguage.Text.ToString() + "<18>"
                        + "<19>" + this.txtSignature.Text.ToString() + "<19><0>" + this.listBox1.SelectedIndex + "<0>");

                    sw.Close();
                }


            }
            using (StreamReader sr = File.OpenText(path))
            {
                string s = "";
                while ((s = sr.ReadLine()) != null)
                {
                    Console.WriteLine(s);
                }
            }
            
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            if (IsLoadBmp)
            {
                Rectangle rect = new Rectangle(0, 0, bmp.Width , bmp.Height  );
                rect.X = newPoint.X;
                rect.Y = newPoint.Y;
                rect.Width = (int)(rect.Width * zoom);
                rect.Height = (int)(rect.Height * zoom);
                g.FillRectangle(Brushes.White, pictureBox1.ClientRectangle);
                g.DrawImage(bmp, rect, new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofg = new OpenFileDialog();
            //string filenames[] = ofg.FileNames;
            ofg.Filter = "图片文件(*.tif)|*.bmp;*.jpg;*.jpeg;*.png;*.gif;*.tif";
            ofg.Multiselect = true;
            if (ofg.ShowDialog() == DialogResult.OK)
            {
                Form1_Load(null, null);
                Image img = Image.FromFile(ofg.FileName);
                listBox1.Items.AddRange(ofg.FileNames);
                bmp = new Bitmap(img);
                if (pictureBox1.Width < bmp.Width || pictureBox1.Height < bmp.Height)
                zoom = Math.Min((float)pictureBox1.Width / (float)bmp.Width, (float)pictureBox1.Height / (float)bmp.Height);
                else
                zoom = 1.0f;
                img.Dispose();
                IsLoadBmp = true;
                pictureBox1.Refresh();
            }
        }

        private void txtMiddleName_MouseHover(object sender, EventArgs e)
        {
            this.txtMiddleName.BackColor = System.Drawing.SystemColors.InactiveCaption;
         
        }

        private void txtMiddleName_MouseLeave(object sender, EventArgs e)
        {
            this.txtMiddleName.BackColor = System.Drawing.SystemColors.Window;
         
        }

        public void bianhuiyanse()
        {
            this.txtFirstName.BackColor = Color.White;
            this.txtMiddleName.BackColor = Color.White;
            this.txtLastName.BackColor = Color.White;
            this.txtSuffix.BackColor = Color.White;
            this.txtZCS.BackColor = Color.White;
            this.txtaddress1.BackColor = Color.White;
            this.txtaddress2.BackColor = Color.White;
            this.txtMobilePhone.BackColor = Color.White;
            this.txtProvider.BackColor = Color.White;
            this.txtYBlank.BackColor = Color.White;
            this.txtHomePhone.BackColor = Color.White;
            this.txtMHblank.BackColor = Color.White;
            this.txtEmail.BackColor = Color.White;
            this.txtDateOfBirth.BackColor = Color.White;
            this.txtGender.BackColor = Color.White;
            this.txtSighDate.BackColor = Color.White;
            this.txtCountry.BackColor = Color.White;
            this.txtLanguage.BackColor = Color.White;
            this.txtSignature.BackColor = Color.White;
        }
             private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
                {
                    bianhuiyanse();
                    this.txtFirstName.Text = "";
                    this.txtMiddleName.Text = "";
                    this.txtLastName.Text = "";
                    this.txtSuffix.Text = "";
                    this.txtZCS.Text = "";
                    this.txtaddress1.Text = "";
                    this.txtaddress2.Text = "";
                    this.txtMobilePhone.Text = "";
                    this.txtProvider.Text = "";
                    this.txtYBlank.Text = "";
                    this.txtHomePhone.Text = "";
                    this.txtMHblank.Text = "";
                    this.txtEmail.Text = "";
                    this.txtDateOfBirth.Text = "";
                    this.txtGender.Text = "";
                    this.txtSighDate.Text = "";
                    this.txtCountry.Text = "";
                    this.txtLanguage.Text = "";
                    this.txtSignature.Text = "";



                 if (this.listBox1.SelectedIndex != -1) //必要
                 {

                     this.ReadData(this.listBox1.SelectedIndex);


                //this.textbox1.Text = this.listBox1.SelectedItem.ToString();
                Image img = Image.FromFile(this.listBox1.SelectedItem.ToString());
                 bmp = new Bitmap(img);
                if (pictureBox1.Width < bmp.Width || pictureBox1.Height < bmp.Height)
                    zoom = Math.Min((float)pictureBox1.Width / (float)bmp.Width, (float)pictureBox1.Height / (float)bmp.Height);
                else
                    zoom = 1.0f;
                //pictureBox1.Image = Image.FromFile(this.listBox1.SelectedItem.ToString());
                img.Dispose();
                IsLoadBmp = true;
                this.pictureBox1.Refresh();
                //list = new Bitmap(img);
                //IsLoadList = true;
            }
            //Image list = Image.FromFile(this.listBox1.SelectedItems[this.listBox1.SelectedIndex]);
            //IsLoadList = true;ListBox.Items[ListBox.SelectedIndex].Text
        }

             private void pnlTips_MouseDown(object sender, MouseEventArgs e)
             {
                 //获取鼠标的相对坐标
                 mouseOffset =new System.Drawing.Point(e.X, e.Y);
                 DrawingFilmDbSelectview = false;

                 if (e.Y < 10)
                 {
                     DrawingFilmDbSelectview = true;
                 }

             }

             private void pnlTips_MouseMove(object sender, MouseEventArgs e)
             {
                 if ((e.Button == MouseButtons.Left) && (DrawingFilmDbSelectview == true))
                 {
                     //获得鼠标移动尺度
                     System.Drawing.Point moveMousePoint = new System.Drawing.Point((e.X - mouseOffset.X), (e.Y - mouseOffset.Y));//((Control.MousePosition.X + mouseOffset.X), (Control.MousePosition.Y + mouseOffset.Y));
                     //panel坐标跟随变化
                     pnlTips.Location= new System.Drawing.Point(pnlTips.Location.X + moveMousePoint.X,pnlTips.Location.Y + moveMousePoint.Y);                    
                 }
             }

             private void txtFirstName_Leave_1(object sender, EventArgs e)
             {
                 TextBox textBox = sender as TextBox;
                 textBox.BackColor = Color.White;
             }

             private void button3_Click(object sender, EventArgs e)//图片旋转
             {
               
                  try
                 {
                     if (pictureBox1.Image != null)
                     {
                         pictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);
                         Invalidate();
                         pictureBox1.Refresh();

                     }
                 }
                 catch (Exception)
                 {
                     MessageBox.Show("系统错误!", "档案管理系统");
                 }
                 
                 //this.pictureBox1.Refresh();
                 //this.pictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);

               // this.pictureBox1.Refresh();
               
                // this.modifiedBitmap = BitmapManipulator.RotateBitmapRight90(this.modifiedBitmap);
                // ImageRotate ImageRotate = new ImageRotate();
                // this.pictureBox1.Image = ImageRotate.RotateImg(this.pictureBox1.Image,-90);
             }
            


             private void panel2_Paint(object sender, PaintEventArgs e)
             {

             }

             private void Form1_KeyDown(object sender, KeyEventArgs e)
             {
                 Control c = this.ActiveControl;   
                 string typename = c.GetType().ToString();
                 switch (typename)
                 {
                     case "System.Windows.Forms.TextBox":
                         {
                             TextBox text = c as TextBox;
                             if (e.KeyData == Keys.Return)
                             {
                                 SelectNextControl(c, true, true, true, true);
                                 break;
                             }
                             else if (e.KeyData == Keys.Down)
                             {
                                 textBox.Text = this.lbldierci.Text;
                                 break;
                             }
                             else if (e.KeyData == Keys.Up)
                             {
                                 textBox.Text = this.lbldiyici.Text;
                                 break;
                             }
                             else if (e.KeyData == Keys.Left)
                             {
                                 if (text.SelectionStart == 0)
                                 {
                                     SelectNextControl(c, false, true, true, true);
                                 }
                                 break;
                             }
                             else if (e.KeyData == Keys.Right)
                             {
                                 if (text.SelectionStart == text.Text.Length)
                                 {
                                     SelectNextControl(c, true, true, true, true);
                                 }
                                 break;
                             }
                             break;
                         }
                     case "System.Windows.Forms.DateTimePicker":
                         {
                             DateTimePicker time = c as DateTimePicker;
                             if (e.KeyData == Keys.Return)
                             {
                                 SelectNextControl(c, true, true, true, true);
                                 break;
                             }
                             break;

                         }
                     case "System.Windows.
                         {
                             ComboBox text = c as ComboBox;
                             if (e.KeyData == Keys.Return)
                             {
                                 SelectNextControl(c, true, true, true, true);
                                 break;
                             }
                             else if (e.KeyData == Keys.Left)
                             {
                                 if (text.SelectionStart == 0)
                                 {
                                     SelectNextControl(c, false, true, true, true);
                                 }
                                 break;
                             }
                             else if (e.KeyData == Keys.Right)
                             {
                                 if (text.SelectionStart == text.Text.Length)
                                 {
                                     SelectNextControl(c, true, true, true, true);
                                 }
                                 break;
                             }
                             break;
                         }
                 }

             }         
    }
}
2011-03-17 11:48
kenshin5868
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-7-31
得分:0 
这个就是我的完整代码了,高手看下哈,为什么我这个图片不能旋转呀?
2011-03-17 11:49



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-333727-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.069427 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved