从onPaint方法保存C#图像

时间:2010-05-25 04:21:48

标签: c# image save windows-mobile-6

大家好知道如何从我创建的矩形中保存图像?

    protected override void OnPaint(PaintEventArgs e)
    {
        Bitmap bitmap = new Bitmap(@"Pictures/testing.jpg");
        Image img = bitmap;

       int width = testing.Width / 3;
       int height = testing.Height / 3;
       Rectangle destrect = new Rectangle(0, 0, width, height);
       GraphicsUnit units = GraphicsUnit.Pixel;
       System.Drawing.Imaging.ImageAttributes imageAttr= new System.Drawing.Imaging.ImageAttributes();

        //1.1.jpg//
       //e.Graphics.DrawImage(img,destrect,0,0, width, height, units, imageAttr);

        //1.2.jpg//
      e.Graphics.DrawImage(img, destrect, width, 0,width, height, units, imageAttr);

        base.OnPaint(e);

    }

我有所需的图像被裁剪但我不知道如何保存..非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

无论如何,我自己得到了,这是按钮点击事件中的简单。

private void button1_Click(object sender,EventArgs e)         {             位图lol = new Bitmap(100,100); ;

        GraphicsUnit units = GraphicsUnit.Pixel;
        System.Drawing.Imaging.ImageAttributes imageAttr = new System.Drawing.Imaging.ImageAttributes();

        Graphics gx = Graphics.FromImage(lol);


        int width = testing.Width / 3;
        int height = testing.Height / 3;
        Rectangle destrect1 = new Rectangle(0, 0, width, height);
           //1.1.jpg//
        gx.DrawImage(testing, destrect1, 0, 0, width, height, units, imageAttr);

        pictureBox2.Image = lol;
        lol.Save(@"Pictures\\hahaha.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
       // pictureBox2.Image = gx.(lol); 

    }

其中testing是我在构造函数中声明的原始位图图像。