1,105,416 Community Members

Help with image editing

Member Avatar
RenanLazarotto
Posting Whiz in Training
262 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
0
 

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!

Member Avatar
Momerath
Senior Poster
3,825 posts since Aug 2010
Reputation Points: 1,323 [?]
Q&As Helped to Solve: 662 [?]
Skill Endorsements: 19 [?]
Featured
 
0
 

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

Bitmap b = CropBitmap(...);

b.Save("C:/myfile.bmp");
Member Avatar
RenanLazarotto
Posting Whiz in Training
262 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
0
 

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

I'll try it later (:

Member Avatar
RenanLazarotto
Posting Whiz in Training
262 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Question Answered as of 2 Years Ago by Momerath
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article