We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,437 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Help with image editing

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!

2
Contributors
3
Replies
22 Hours
Discussion Span
1 Year Ago
Last Updated
4
Views
Question
Answered
RenanLazarotto
Posting Whiz in Training
263 posts since Nov 2010
Reputation Points: 16
Solved Threads: 11
Skill Endorsements: 0

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

Bitmap b = CropBitmap(...);

b.Save("C:/myfile.bmp");
Momerath
Senior Poster
3,729 posts since Aug 2010
Reputation Points: 1,322
Solved Threads: 624
Skill Endorsements: 13

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

I'll try it later (:

RenanLazarotto
Posting Whiz in Training
263 posts since Nov 2010
Reputation Points: 16
Solved Threads: 11
Skill Endorsements: 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.

RenanLazarotto
Posting Whiz in Training
263 posts since Nov 2010
Reputation Points: 16
Solved Threads: 11
Skill Endorsements: 0
Question Answered as of 1 Year Ago by Momerath

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0621 seconds using 2.65MB