【问题标题】:Rotate image using asp.net c# through textbox通过文本框使用asp.net c#旋转图像
【发布时间】:2013-12-20 17:47:59
【问题描述】:

如何通过在 asp.net c# 中使用图形在文本框中输入角度值来旋转图像? 我拿了一个文本框和图像和按钮。我想在文本框中给出角度值,图像将根据给定的值旋转。所以,我只能做一个简单的 button_click 事件,但它不适合 textbox.so,给简单的代码...

protected void Button2_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath(Image1.ImageUrl);

        ////create an image object from the image in that path
        System.Drawing.Image img = System.Drawing.Image.FromFile(path);

        ////rotate the image
       img.RotateFlip(RotateFlipType.Rotate90FlipXY);

        ////save the image out to the file
        img.Save(path);

        ////release image file
        img.Dispose();

    } 

【问题讨论】:

    标签: c# asp.net image-processing gdi+ gdi


    【解决方案1】:

    以下代码将旋转后的图片保存到当前项目文件夹路径下的newfile.png中

    试试这个:

     protected void Button2_Click1(object sender, EventArgs e)
        {
    
             string path = Server.MapPath(Image1.ImageUrl);
             string newpath =Server.MapPath(@"~/filename.png");
    
            ////create an image object from the image in that path
            System.Drawing.Bitmap img =new  System.Drawing.Bitmap(path);      
    
            Bitmap map = RotateBitmap(COnvert.ToSingle(textBox1.Text), img);
            map.Save(newpath);
    
    
    
    
            ////release image file
            img.Dispose();
        }
         public Bitmap RotateBitmap(float Angle, Bitmap bm_in)
        {
            try
            {
                float wid = bm_in.Width;
                float hgt = bm_in.Height;
                Point[] corners = { new Point(0, 0), new Point(int.Parse(wid.ToString()), 0), new Point(0, int.Parse(hgt.ToString())), new Point(int.Parse(wid.ToString()), int.Parse(hgt.ToString())) };
                int cx = int.Parse(wid.ToString()) / 2;
                int cy = int.Parse(hgt.ToString()) / 2;
                long i;
                for (i = 0; i <= 3; i++)
                {
                    corners[i].X -= Convert.ToInt32(cx.ToString());
                    corners[i].Y -= Convert.ToInt32(cy.ToString());
                }
    
    
                float theta = (float)(Angle * Math.PI / 180.0);
                float sin_theta = (float)Math.Sin(theta);
                float cos_theta = (float)Math.Cos(theta);
                float X;
                float Y;
                for (i = 0; i <= 3; i++)
                {
                    X = corners[i].X;
                    Y = corners[i].Y;
                    corners[i].X = (int)(X * cos_theta + Y * sin_theta);
                    corners[i].Y = (int)(-X * sin_theta + Y * cos_theta);
                }
    
    
                float xmin = corners[0].X;
                float ymin = corners[0].Y;
                for (i = 1; i <= 3; i++)
                {
                    if (xmin > corners[i].X)
                        xmin = corners[i].X;
                    if (ymin > corners[i].Y)
                        ymin = corners[i].Y;
                }
                for (i = 0; i <= 3; i++)
                {
                    corners[i].X -= int.Parse(xmin.ToString());
                    corners[i].Y -= int.Parse(ymin.ToString());
                }
    
    
                Bitmap bm_out = new Bitmap((int)(-2 * xmin), (int)(-2 * ymin));
                Graphics gr_out = Graphics.FromImage(bm_out);
                // ERROR: Not supported in C#: ReDimStatement
                Point[] temp = new Point[3];
                if (corners != null)
                {
                    Array.Copy(corners, temp, Math.Min(corners.Length, temp.Length));
                }
                corners = temp;
                gr_out.DrawImage(bm_in, corners);
                return bm_out;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                return bm_in;
            }
        }
    

    【讨论】:

    • 先生,如果我在 4 或 180 中输入文本框然后发生错误:-GDI+ 中发生一般错误,JPEG 图像到 MemoryStream.so,请给出建议..
    • 先生,如果是 1 到 80 度角,那么如何确定它们的角度,也是 100 到 170 度,也是 190 到 260 度?给出响应?
    • @user3065044:所以请告诉我你将如何输入输入是在 1-80、80-270 这样的范围内吗?
    • @user3065044 :但我不认为如何传递它们,因为可用的类型很少是 90,180,270 => RotateFlipType.Rotate90FlipXY,RotateFlipType.Rotate180FlipXY,RotateFlipType.Rotate270FlipXY
    • 不,我不想要固定角度.. 我需要提供给它的角度,并由它旋转图像。
    猜你喜欢
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-16
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多