C# 通过 onPaint 方法保存图像

发布于 2024-09-03 00:09:23 字数 781 浏览 9 评论 0原文

嗨,有人知道如何保存我创建的矩形中的图像吗?

    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);

    }

我有裁剪后的所需图像,但我不知道如何保存..将不胜感激任何帮助。

Hi anyone knows how to save an image from the rectangle i created?

    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);

    }

I have the desired image that is cropped but i don't know how to save .. Would greatly appreciate any help .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉酒的小男人 2024-09-10 00:09:23

不管怎样,我自己得到了它,这是按钮点击事件的简单事件。

私人无效button1_Click(对象发送者,EventArgs e)
{
位图 哈哈 = 新位图(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); 

    }

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

anyhow, i got it myself, this is the simple one on button click event.

private void button1_Click(object sender, EventArgs e)
{
Bitmap 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); 

    }

where testing is my original bitmap image declared in constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文