Results 1 to 14 of 14

Thread: [RESOLVED] Photo Manipulation

  1. #1

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Resolved [RESOLVED] Photo Manipulation

    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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Photo Manipulation

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: Photo Manipulation

    Here is what i have come up with so far



    c# Code:
    1. OpenFileDialog OFD = new OpenFileDialog();
    2.             OFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    3.             OFD.Filter = "Photo Jpeg (*.jpg)|*.jpg";
    4.             OFD.FilterIndex = 1;
    5.  
    6.             if (OFD.ShowDialog() == DialogResult.OK)
    7.             {
    8.                 pictureBox1.ImageLocation = OFD.FileName;
    9.             }

    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



    The photo obvously doesnt apply but its the only photo i had handy at home
    Last edited by Crash893; Oct 26th, 2007 at 04:19 PM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Photo Manipulation

    After that code the Image property of your PictureBox will refer to the Image object that you have loaded your image file into.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: Photo Manipulation

    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?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Photo Manipulation

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: Photo Manipulation

    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

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Photo Manipulation

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: Photo Manipulation

    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)

  10. #10

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: Photo Manipulation

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

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Photo Manipulation

    That VB code:
    vb.net Code:
    1. Public Sub New()
    2.     Me.New(Point.Empty, Point.Empty)
    3. End Sub
    is invoking a constructor from a constructor. In C# that looks like this:
    csharp Code:
    1. public MyClass() : this(Point.Empty, Point.Empty)
    2. {
    3. }
    where MyClass is the class name. Constructors in C# are always named after the type, not "New".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: Photo Manipulation

    OHHHHHHHhhhh

    I was way off

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

  13. #13

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: Photo Manipulation

    Here is what i got so far

    Still not working but no error messages.

    c# Code:
    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Text;
    7. using System.Windows.Forms;
    8.  
    9. namespace crop2
    10. {
    11.     public partial class Form1 : Form
    12.     {
    13.         public Form1()
    14.         {
    15.             InitializeComponent();
    16.         }
    17.  
    18.      
    19.  
    20.  
    21.     //The lines that have been drawn but not saved.
    22.     private List<Line> lines = new List<Line>();
    23.     //The start point of the line currently being drawn.
    24.     private Point start;
    25.  
    26.     private void Form1_Load(object sender, System.EventArgs e)
    27.     {
    28.         //Place a blank image in the PictureBox control.
    29.         this.pictureBox1.Image = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
    30.     }
    31.  
    32.     private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    33.     {
    34.         //Remember the point at which the line started.
    35.         this.start = e.Location;
    36.     }
    37.     private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    38.     {
    39.         //Remember the point at which the line ended.
    40.         Point end = e.Location;
    41.         //Add the new line to the list.
    42.         this.lines.Add(new Line(this.start, end));
    43.         //Force the control to repaint so the new line is drawn.
    44.         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)));
    45.         this.pictureBox1.Update();
    46.  
    47.     }
    48.     private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    49.     {
    50.         //Draw each line on the control.
    51.         foreach (Line line in this.lines)
    52.         {
    53.             e.Graphics.DrawLine(Pens.Black, line.Start, line.End);
    54.         }
    55.     }
    56.     private void Save()
    57.     {
    58.  
    59.         //Create a Graphics object from the Image in the PictureBox.
    60.         Graphics g = Graphics.FromImage(this.pictureBox1.Image);
    61.  
    62.         //Draw each line on the image to make them permanent.
    63.         foreach (Line line in this.lines)
    64.         {
    65.             g.DrawLine(Pens.Black, line.Start, line.End);
    66.         }
    67.         //Clear the temporary lines that were just saved.
    68.         this.Clear();
    69.  
    70.     }
    71.     private void Clear()
    72.     {
    73.         //Clear all unsaved lines.
    74.         this.lines.Clear();
    75.  
    76.         //Force the control to repaint so the lines are removed.
    77.         this.pictureBox1.Refresh();
    78.     }
    79. }
    80.  
    81. public class Line
    82. {
    83.  
    84.     //The line's start point.
    85.     private Point _start;
    86.  
    87.     //The line's end point.
    88.     private Point _end;
    89.  
    90.     //The line's start point.
    91.     public Point Start
    92.     {
    93.         get { return this._start; }
    94.         set { this._start = value; }
    95.     }
    96.  
    97.     //The line's end point.
    98.     public Point End
    99.     {
    100.         get { return this._end; }
    101.         set { this._end = value; }
    102.     }
    103.      
    104.       public Line():this(Point.Empty, Point.Empty)
    105.          {
    106.          }
    107.     public Line(Point start, Point end)
    108.     {
    109.         this._start = start;
    110.         this._end = end;
    111.     }
    112. }
    113. }

  14. #14

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: Photo Manipulation

    Rather than reinvent the wheel

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width