Hello guys!
I'm having such a trouble with editing a image.

This is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace Removedor_de_Banners
{
    public partial class Form1 : Form
    {
        string caminho = null;
        int largura = 0;
        int altura = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnImg_Click(object sender, EventArgs e)
        {
            if (abrirImg.ShowDialog() == DialogResult.OK)
            {
                caminho = abrirImg.FileName;
                boxCaminho.Text = caminho;
                Image tamanho = Image.FromFile(caminho);

                lblAltura.Text = "Altura: " + tamanho.Height.ToString() + "px";
                lblLargura.Text = "Largura: " + tamanho.Width.ToString() + "px";

                altura = tamanho.Height;
                largura = tamanho.Width;
            }
        }

        public Bitmap CropBitmap(Bitmap bitmap, int cropX, int cropY, int cropWidth, int cropHeight)
        {
            Rectangle rect = new Rectangle(cropX, cropY, cropWidth, cropHeight);
            Bitmap cropped = bitmap.Clone(rect, bitmap.PixelFormat);
            return cropped;
        }
    }
}

The problem is... I don't know how to save the returned bitmap! The code isn't completed at all, but I want to make the save function first. Can you guys give me some help?

Thanks!

Recommended Answers

All 3 Replies

Bitmap has a save method, so you could do something like this:

Bitmap b = CropBitmap(...);

b.Save("C:/myfile.bmp");

So, instead of return cropped; I could use b.Save("C:/myfile.bmp"); ?

I'll try it later (:

Thanks a lot! Here is what I'm using:

public Bitmap CropBitmap(Bitmap bitmap, int cropX, int cropY, int cropWidth, int cropHeight)
        {
            Rectangle rect = new Rectangle(cropX, cropY, cropWidth, cropHeight);
            Bitmap cropped = bitmap.Clone(rect, bitmap.PixelFormat);
            return cropped;
        }

        private void cmdProcessar_Click(object sender, EventArgs e)
        {
            Bitmap ImagemPraCortar = new Bitmap(caminho);
            Bitmap ImagemCortada = CropBitmap(ImagemPraCortar, 0, 0, largura, altura - 50);
            ImagemCortada.Save("C:\\teste2.png");
        }

Please tell me if something is wrong, but problem solved! Might be wrong, but it is working! Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.