Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows Application
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
 
namespace CanonImageVB.Classes
{
    class UserRect
    {
 
<pre>
    private PictureBox mPictureBox;
    public static Rectangle rect;
    public bool allowDeformingDuringMovement = false;
    private bool mIsClick = false;
    private bool mMove = false;
    private int oldX;
    private int oldY;
    private int sizeNodeRect = 5;
    private Bitmap mBmp = null;
    private PosSizableRect nodeSelected = PosSizableRect.None;
    private int angle = 30;
    public static int Width;
    public static int Height;
    public static int CurrX;
    public static int CurrY;
    //public static int gridSize;
    public static int SGridLine;
    private enum PosSizableRect
    {
        UpMiddle,
        LeftMiddle,
        LeftBottom,
        LeftUp,
        RightUp,
        RightMiddle,
        RightBottom,
        BottomMiddle,
        None
 
    };
 
    public UserRect(Rectangle r)
    {
        rect = r;
        mIsClick = false;
    }
 
    public void Draw(Graphics g)
    {
        Pen  p = new Pen(Color.Red, 4);
        g.DrawRectangle(p,rect);
 
        foreach (PosSizableRect pos in Enum.GetValues(typeof(PosSizableRect)))
        {
            g.DrawRectangle(p, GetRect(pos));
        }
 
        int gridSize = 30;
        int x = gridSize;
        Pen WPen = new Pen(System.Drawing.Color.White,3);
        Pen BPen = new Pen(System.Drawing.Color.Black, 3);
        int i = 0;
 
        int y = gridSize;
        int xx = 0, yy = 0;
        xx = 565 / 2;
        yy = 415 / 2;
        if (SGridLine == 1)
        {
            g.DrawLine(BPen, xx, 0, xx, 415);
            g.DrawLine(WPen, 0, yy,565, yy);
        }
 
        else if (SGridLine == 2)
        {
            while (x &lt; 415)
            {
                if (i % 2 == 0)
                {
                    g.DrawLine(BPen, x, 0, x, 565);
                }
                else
                {
                    g.DrawLine(WPen, x, 0, x, 565);
                }
                x += gridSize;
                i = i + 1;
            }
 
            i = 0;
            while (y &lt; 415)
            {
                if (i % 2 == 0)
                {
                    g.DrawLine(WPen, 0, y, 565, y);
                }
                else
                {
                    g.DrawLine(BPen, 0, y, 565, y);
                }
                y += gridSize;
                i = i + 1;
            }
 
        }
 
    }
 
    public void SetBitmapFile(string filename)
    {
        this.mBmp = new Bitmap(filename);
    }
 
    public void SetBitmap(Bitmap bmp)
    {
        this.mBmp = bmp;
    }
 
    public void SetPictureBox(PictureBox p)
    {
        this.mPictureBox = p;
        mPictureBox.MouseDown += new MouseEventHandler(mPictureBox_MouseDown);
        mPictureBox.MouseUp += new MouseEventHandler(mPictureBox_MouseUp);
        mPictureBox.MouseMove += new MouseEventHandler(mPictureBox_MouseMove);
        mPictureBox.Paint += new PaintEventHandler(mPictureBox_Paint);
    }
    public static Image AddGridlines(Image img, int gridSize, int SGridLine)
    {
 
        Graphics g = Graphics.FromImage(img);
 
        int x = gridSize;
        Pen WPen = new Pen(System.Drawing.Color.White,-100);
        Pen BPen = new Pen(System.Drawing.Color.Black, 3);
        int i = 0;
 
        int y = gridSize;
        int xx = 0, yy = 0;
        xx = img.Width / 2;
        yy = img.Height / 2;
        if (SGridLine == 1)
        {
            g.DrawLine(BPen, xx, 0, xx, img.Height);
            g.DrawLine(WPen, 0, yy, img.Width, yy);
        }
        else if (SGridLine == 2)
        {
            while (x &lt; img.Width)
            {
                if (i % 2 == 0)
                {
                    g.DrawLine(BPen, x, 0, x, img.Height);
                }
                else
                {
                    g.DrawLine(WPen, x, 0, x, img.Height);
                }
                x += gridSize;
                i = i + 1;
            }
 
            i = 0;
            while (y &lt; img.Height)
            {
                if (i % 2 == 0)
                {
                    g.DrawLine(WPen, 0, y, img.Width, y);
                }
                else
                {
                    g.DrawLine(BPen, 0, y, img.Width, y);
                }
                y += gridSize;
                i = i + 1;
            }
 
        }
        return img;
 
    }
 
    private void mPictureBox_Paint(object sender, PaintEventArgs e)
    {
        try
        {
            Draw(e.Graphics);
        }
        catch (Exception exp)
        {
            System.Console.WriteLine(exp.Message);
        } 
    }
 
    private void mPictureBox_MouseDown(object sender, MouseEventArgs e)
    {
        mIsClick = true;
 
        nodeSelected = PosSizableRect.None;
        nodeSelected = GetNodeSelectable(e.Location);
 
        if (rect.Contains(new Point(e.X, e.Y)))
        {
            mMove = true;
        }
        oldX = e.X;
        oldY = e.Y;
    }
 
    private void mPictureBox_MouseUp(object sender, MouseEventArgs e)
    {
        mIsClick = false;
        mMove = false;
    }
    Point pt;
    private void mPictureBox_MouseMove(object sender, MouseEventArgs e)
    {
 
       ChangeCursor(e.Location);
 
        if (mIsClick == false)
        {
            return;
        }
 
        Rectangle backupRect = rect;
 
        switch (nodeSelected)
        {
            case PosSizableRect.LeftUp:
                rect.X += e.X - oldX;
                rect.Width -= e.X - oldX;
 
                rect.Y += e.Y - oldY;
                rect.Height -= e.Y - oldY;
                break;
            case PosSizableRect.LeftMiddle:
                rect.X += e.X - oldX;
                rect.Width -= e.X - oldX;
                break;
            case PosSizableRect.LeftBottom:
                rect.Width -= e.X - oldX;
                rect.X += e.X - oldX;
                rect.Height += e.Y - oldY;
                break;
            case PosSizableRect.BottomMiddle:
                rect.Height += e.Y - oldY;
                break;
            case PosSizableRect.RightUp:
                rect.Width += e.X - oldX;
                rect.Y += e.Y - oldY;
                rect.Height -= e.Y - oldY;
                break;
            case PosSizableRect.RightBottom:
                rect.Width += e.X - oldX;
                rect.Height += e.Y - oldY;
                break;
            case PosSizableRect.RightMiddle:
                rect.Width += e.X - oldX;
                break;
 
            case PosSizableRect.UpMiddle:
                rect.Y += e.Y - oldY;
                rect.Height -= e.Y - oldY;
                break;
 
            default:
 
                if (mMove)
                {
 
                    rect.X = rect.X + e.X - oldX;
 
                    rect.Y = rect.Y + e.Y - oldY;
 
                }
                break;
        }
        oldX = e.X;
        oldY = e.Y;
 
        if (rect.Width &lt; 50 || rect.Height &lt; 50)
        {
            rect = backupRect;
        }
 
        TestIfRectInsideArea();
 
        mPictureBox.Invalidate();
        Width = rect.Width;
        Height = rect.Height;
        CurrX = rect.X;
        CurrY = rect.Y;
 
    }
 
    private void TestIfRectInsideArea()
    {
        // Test if rectangle still inside the area.
        if (rect.X &lt; 0) rect.X = 0;
        if (rect.Y &lt; 0) rect.Y = 0;
        if (rect.Width &lt;= 0) rect.Width = 1;
        if (rect.Height &lt;= 0) rect.Height = 1;
 
        if (rect.X + rect.Width &gt; mPictureBox.Width)
        {
            rect.Width = mPictureBox.Width - rect.X ; // -1 to be still show 
            if (allowDeformingDuringMovement == false)
            {
                mIsClick = false;
            }
        }
        if (rect.Y + rect.Height &gt; mPictureBox.Height)
        {
            rect.Height = mPictureBox.Height - rect.Y ;// -1 to be still show 
            if (allowDeformingDuringMovement == false)
            {
                mIsClick = false;
            }
        }
    }
 
    private Rectangle CreateRectSizableNode(int x, int y)
    {
        return new Rectangle(x - sizeNodeRect / 2, y - sizeNodeRect / 2, sizeNodeRect, sizeNodeRect);
    }
 
    private Rectangle GetRect(PosSizableRect p)
    {
        switch (p)
        {
            case PosSizableRect.LeftUp:
                return CreateRectSizableNode(rect.X, rect.Y);
 
            case PosSizableRect.LeftMiddle:
                return CreateRectSizableNode(rect.X, rect.Y + +rect.Height / 2);
 
            case PosSizableRect.LeftBottom:
                return CreateRectSizableNode(rect.X, rect.Y + rect.Height);
 
            case PosSizableRect.BottomMiddle:
                return CreateRectSizableNode(rect.X + rect.Width / 2, rect.Y + rect.Height);
 
            case PosSizableRect.RightUp:
                return CreateRectSizableNode(rect.X + rect.Width, rect.Y);
 
            case PosSizableRect.RightBottom:
                return CreateRectSizableNode(rect.X + rect.Width, rect.Y + rect.Height);
 
            case PosSizableRect.RightMiddle:
                return CreateRectSizableNode(rect.X + rect.Width, rect.Y + rect.Height / 2);
 
            case PosSizableRect.UpMiddle:
                return CreateRectSizableNode(rect.X + rect.Width / 2, rect.Y);
            default:
                return new Rectangle();
        }
    }
 
    private PosSizableRect GetNodeSelectable(Point p)
    {
        foreach (PosSizableRect r in Enum.GetValues(typeof(PosSizableRect)))
        {
            if (GetRect(r).Contains(p))
            {
                return r;
            }
        }
        return PosSizableRect.None;
    }
 
    private void ChangeCursor(Point p)
    {
        mPictureBox.Cursor = GetCursor(GetNodeSelectable(p));
    }
 
    /// &lt;summary&gt;
    /// Get cursor for the handle
    /// &lt;/summary&gt;
    /// &lt;param name="p"&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    private Cursor GetCursor(PosSizableRect p)
    {
        switch (p)
        {
            case PosSizableRect.LeftUp:
                return Cursors.SizeNWSE;
 
            case PosSizableRect.LeftMiddle:
                return Cursors.SizeWE;
 
            case PosSizableRect.LeftBottom:
                return Cursors.SizeNESW;
 
            case PosSizableRect.BottomMiddle:
                return Cursors.SizeNS;
 
            case PosSizableRect.RightUp:
                return Cursors.SizeNESW;
 
            case PosSizableRect.RightBottom:
                return Cursors.SizeNWSE;
 
            case PosSizableRect.RightMiddle:
                return Cursors.SizeWE;
 
            case PosSizableRect.UpMiddle:
                return Cursors.SizeNS;
            default:
                return Cursors.Default;
        }
    }
 
}

}
Posted 12-Dec-12 21:00pm
Edited 12-Dec-12 21:06pm
Jibesh16.8K
v2
Comments
Member 9021661 at 13-Dec-12 2:03am
   
as when croping going to be done first time it is fine working but from next when mouse moved a little bit crop moving faster than mouse. mouse and crop are not at same place they are a part from each other..please give the solution to me as soon as possible..
thanks in advance


regards

dotnetdeveloper.
Christian Graus at 13-Dec-12 2:14am
   
This is not a real question. It's a code dump with an insanely long subject, but not enough detail to explain what the issue is

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Please look this sample application.

http://code.msdn.microsoft.com/windowsapps/CSWin8AppCropBitmap-52fa1ad7[^]

Thanks,

Bilaal
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 395
2 Maciej Los 285
3 ProgramFOX 265
4 CHill60 200
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 395
2 Maciej Los 285
3 ProgramFOX 265
4 CHill60 200


Advertise | Privacy | Mobile
Web01 | 2.8.150331.1 | Last Updated 12 Sep 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100