PDA

Click to See Complete Forum and Search --> : [RESOLVED] Photo Manipulation


Crash893
Oct 25th, 2007, 03:02 PM
Hi all

I just got tapped for another project and im not sure what direction im going to take to get it done but here it is in a nut shell


we currently have adobe photoshop that we have "actions" stored for that are basically macros. The macros basically prompt the user for a series of crop dimensions and then outputs the data to network folder


so its probably using .000001% of photoshops power.


So my question would how hard is it to set up a series "prompts" to crop in C# Is there something already setup to do this like photo.crop(x,y,x1,y1) or would i have to figure out the entire thing from scratch?

jmcilhinney
Oct 25th, 2007, 10:06 PM
Assuming you're using an image format supported by the Image class, you'd load the original file into an Image object, create a new Bitmap object of the desired dimensions, create a Graphics object for the new Bitmap, then draw the original Image onto the new Bitmap using the DrawImage method of the Graphics object. There are numerous overloads that let you control what, where and how the image gets drawn, so you can draw only the section you want to keep. You'd then save the Bitmap and you're done.

Crash893
Oct 26th, 2007, 03:51 PM
Here is what i have come up with so far




OpenFileDialog OFD = new OpenFileDialog();
OFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
OFD.Filter = "Photo Jpeg (*.jpg)|*.jpg";
OFD.FilterIndex = 1;

if (OFD.ShowDialog() == DialogResult.OK)
{
pictureBox1.ImageLocation = OFD.FileName;
}

Very basic I know

My first question is the line

picturebox1.imagelcoation = ofd.filename;

the same as " Load the original file into an Image object"



this is my prototype UI -where each button would have you draw a rectangle and that rectangle would output the cropted image to a network drive

http://i24.tinypic.com/350jk87.png

The photo obvously doesnt apply but its the only photo i had handy at home

jmcilhinney
Oct 26th, 2007, 08:43 PM
After that code the Image property of your PictureBox will refer to the Image object that you have loaded your image file into.

Crash893
Oct 26th, 2007, 11:17 PM
so i would refer to to it as photobox1.image?


Im still feeling like im not getting something here

i want to select the crop by doing something like drawing the crop on the photo ( like any photo editor) how would i do that?

jmcilhinney
Oct 27th, 2007, 03:56 AM
The original Image object is referred to by the PictureBox's Image property, yes.

Follow the Drawing link in my signature for an example of how to draw on a PictureBox. You just have to use the two points to draw a rectangle instead of a line.

Crash893
Oct 27th, 2007, 04:16 PM
I'll be sure to give that a try

I only have c#.express on my home machine so i cant compile it yet

I have vb at work so it will have to wait for sunday/monday

jmcilhinney
Oct 27th, 2007, 06:31 PM
Ah, forgot we were in the C# forum. There are online converters for VB to C#, or you could use Instant C# from Tangible Software (see David Anton's signature). There's not that much code so if you know some VB you should be able to translate it yourself anyway, or you could just download and install VB Express.

Crash893
Oct 27th, 2007, 10:42 PM
True, I have seen and use the c# to vb translater alot but with out both on my machine its hard because if something doesnt translate you can't run the working side to know whats wrong.


as for vb.net your probably right but i think i already burn up enogh of my weekend on work projects.

ill get around to it just not tonight.

Thanks for your help ill probably have questions later (as usual)

Crash893
Oct 29th, 2007, 03:28 PM
Im having trouble with only one line

" Public Sub New()
Me.New(Point.Empty, Point.Empty)
End Sub
"

doesn't seem to want to convert over

Ive been playing around with it for a while and here is what i have so far
It will compile with no errors but its not working yet

public void New(Point x1, Point x2)
{
this.New(Point.Empty, Point.Empty);
}

jmcilhinney
Oct 29th, 2007, 05:25 PM
That VB code:Public Sub New()
Me.New(Point.Empty, Point.Empty)
End Subis invoking a constructor from a constructor. In C# that looks like this:public MyClass() : this(Point.Empty, Point.Empty)
{
}where MyClass is the class name. Constructors in C# are always named after the type, not "New".

Crash893
Oct 29th, 2007, 06:51 PM
OHHHHHHHhhhh

I was way off

I was thinking it was a sub you called "new"

Crash893
Oct 29th, 2007, 06:56 PM
Here is what i got so far

Still not working but no error messages.



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace crop2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}




//The lines that have been drawn but not saved.
private List<Line> lines = new List<Line>();
//The start point of the line currently being drawn.
private Point start;

private void Form1_Load(object sender, System.EventArgs e)
{
//Place a blank image in the PictureBox control.
this.pictureBox1.Image = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
}

private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
//Remember the point at which the line started.
this.start = e.Location;
}
private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
//Remember the point at which the line ended.
Point end = e.Location;
//Add the new line to the list.
this.lines.Add(new Line(this.start, end));
//Force the control to repaint so the new line is drawn.
this.pictureBox1.Invalidate(new Rectangle(Math.Min(this.start.X, end.X), Math.Min(this.start.Y, end.Y), Math.Abs(this.start.X - end.X), Math.Abs(this.start.Y - end.Y)));
this.pictureBox1.Update();

}
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
//Draw each line on the control.
foreach (Line line in this.lines)
{
e.Graphics.DrawLine(Pens.Black, line.Start, line.End);
}
}
private void Save()
{

//Create a Graphics object from the Image in the PictureBox.
Graphics g = Graphics.FromImage(this.pictureBox1.Image);

//Draw each line on the image to make them permanent.
foreach (Line line in this.lines)
{
g.DrawLine(Pens.Black, line.Start, line.End);
}
//Clear the temporary lines that were just saved.
this.Clear();

}
private void Clear()
{
//Clear all unsaved lines.
this.lines.Clear();

//Force the control to repaint so the lines are removed.
this.pictureBox1.Refresh();
}
}

public class Line
{

//The line's start point.
private Point _start;

//The line's end point.
private Point _end;

//The line's start point.
public Point Start
{
get { return this._start; }
set { this._start = value; }
}

//The line's end point.
public Point End
{
get { return this._end; }
set { this._end = value; }
}

public Line():this(Point.Empty, Point.Empty)
{
}
public Line(Point start, Point end)
{
this._start = start;
this._end = end;
}
}
}

Crash893
Oct 29th, 2007, 10:37 PM
Rather than reinvent the wheel

I am going to move over to the vb.net forum