Click here to Skip to main content
12,401,984 members (27,761 online)
Click here to Skip to main content

Tagged as

Stats

80.4K views
9.7K downloads
156 bookmarked
Posted

Sprite Editor for .NET

, 27 Jan 2010 CPOL
Build, edit, and animate your own sprites.
classSprite
classSprite
classSprite.csproj.user
bin
Release
classSprite.dll
Properties
GraphicsEditor CSharp
GraphicsEditor CSharp
graphics editor icon.ico
GraphicsEditor CSharp.csproj.user
Properties
Resources
Brush_Icon.bmp
click to load image.bmp
colorTemplate.bmp
cursor_cross.bmp
cursor_move.bmp
cursor_resizeDiagonal_BL_TR.bmp
cursor_resizeDiagonal_TL_BR.bmp
cursor_resizeLeftRight.bmp
cursor_resizeUpDown.bmp
Edit_Redo_Icon.bmp
Edit_Undo_Icon.bmp
Eraser_Icon.bmp
FloodFill_Icon.bmp
FloodFill_pnlBackground_sameColor.bmp
FloodFill_pnlBackground_toBorder.bmp
Free-Form Select_Icon.bmp
Gun.bmp
Line_Icon.bmp
Magnify_Icon.bmp
Pencil_Icon.bmp
Pick-color_Icon.bmp
Select_Icon.bmp
sprite Editor.jpg
Straighten_Icon.bmp
Night Stalker
Night Stalker
Night Stalker.csproj.user
nightstalker.ico
Properties
Resources
bomb.wav
bulletEaten.wav
BulletEater.spr
bulletEater.wav
bunker.png
bunkerbuster.spr
BunkerBuster.wav
CDbunkerWalls.bmp
Colt Round.png
Colt.spr
colt45.wav
corpse (small).spr
diescream.wav
emptychamber.wav
Evil Jessica (small).spr
Felix the Cat (small).spr
FelixFire.spr
FelixFire.wav
freeLife.wav
gun.png
gun.spr
heartbeat.wav
help_background.jpg
hithard.wav
Jessica Rabbit Small.spr
largeJessica.spr
Laser.spr
laserFire.wav
loadclip.wav
Map WPs.png
Map.jpg
MapCD.bmp
mini bat.spr
NearestWayPointsTable.bfs
nightstalker.bmp
nightstalker.png
red bunker.png
Robot Den New.png
Robot_A.spr
smurf (small).spr
smurf-attack.spr
spider (small).spr
Spider Web.png
splat.wav
WayPointsArray.bfs
WayPointsTable.bfs
Sprites
Robot1.bmp
Robot2.bmp
robot3.bmp
Bat
bat.spr
head.bmp
head.jpg
leftwing.bmp
mini bat.spr
wing.bmp
Bullets
bulletEater original.png
bulletEater.bmp
BulletEater.spr
bunkerbuster.bmp
bunkerbuster.spr
Colt.bmp
Colt.spr
Laser.bmp
Laser.spr
master limb.bmp
smurf-attack.bmp
smurf-attack.spr
Claudia Schiffer
alt.spr
Claudia.spr
Claudia_Arm.bmp
Claudia_Foot.bmp
Claudia_Forearm.bmp
Claudia_Hand_Left.bmp
Claudia_Hand_Right.bmp
Claudia_head.bmp
Claudia_head2.bmp
Claudia_mini.spr
Claudia_Schiffer_1.jpg
Claudia_Shin.bmp
Claudia_Thigh.bmp
Claudia_Torso.bmp
gunHand.bmp
Corpse
blood.bmp
corpse (small).spr
Corpse.spr
guts.bmp
master limb.bmp
robot.png
smurfcorpse.png
Evil Jessica
Evil Jessica (small).spr
Evil Jessica.spr
GunHand(near).bmp
JR_Arm.bmp
JR_Arm.png
JR_Dress.bmp
JR_Dress_resized.bmp
JR_Foot.bmp
JR_Forearm.bmp
JR_Hair(far).bmp
JR_Hair(near).bmp
JR_Hand.bmp
JR_Hand_Right.bmp
JR_Head.bmp
JR_MasterBlock.bmp
JR_Shin.bmp
JR_Thigh_far.bmp
JR_Thigh_near.bmp
JR_Torso.bmp
gun
gun.bmp
gun.spr
master block.bmp
Jessica Rabbit
GunHand(near).bmp
Jessica Rabbit Small.spr
Jessica Rabbit.spr
jessica_rabbit at mic.jpg
JR_Arm.bmp
JR_Dress.bmp
JR_Dress_resized.bmp
JR_Foot.bmp
JR_Forearm.bmp
JR_Hair(far).bmp
JR_Hair(near).bmp
JR_Hand.bmp
JR_Hand_Right.bmp
JR_Head.bmp
JR_MasterBlock.bmp
JR_Shin.bmp
JR_Thigh_far.bmp
JR_Thigh_near.bmp
JR_Torso.bmp
Lindsay Lohan
arm.bmp
foot.bmp
forearm.bmp
gunHand.bmp
Hand_Left.bmp
head.bmp
Lindsay Lohan.spr
Mini Lindsay Lohan.spr
shin.bmp
skirt_ass.bmp
skirt_front.bmp
thigh.bmp
torso.bmp
NightStalkerSprites
BulletEater.spr
bunkerbuster.spr
Colt.spr
corpse (small).spr
Evil Jessica (small).spr
gun.spr
Jessica Rabbit Small.spr
Laser.spr
mini bat.spr
Mini Lindsay Lohan.spr
Robot_A.spr
smurf (small).spr
smurf-attack.spr
spider (small).spr
Robot Heart
RobotHeart_Head.bmp
RobotHeart_Torso.bmp
Robot_EyeShut.bmp
ROBOT_heart.bmp
Robot_Heart.spr
Robot_Heart_small.spr
Robot_LeftArm.bmp
Robot_Leg.bmp
Robot_dumb
Arm.bmp
Arm_right_front.bmp
Hand.bmp
Head.bmp
head_back.bmp
Head_Front.bmp
Leg.bmp
leg_back.bmp
leg_front.bmp
Robot_A.spr
Robot_A_original.spr
Robot_A_original_copy.spr
Torso.bmp
Torso_Front.bmp
Smurf
arm(far).bmp
arm(near).bmp
head.bmp
leg.bmp
smurf (small).spr
smurf.spr
Torso.bmp
spider
body.bmp
eyes shut.bmp
head.bmp
legA_left.bmp
legA_right.bmp
legB_left.bmp
legB_right.bmp
LegC_left.bmp
legC_right.bmp
legD_left.bmp
legD_right.bmp
mandible_left.bmp
mandible_right.bmp
spider (small).spr
spider 2.spr
spider.png
spider.spr
Sprite demo
Sprite demo
Sprite demo.csproj.user
bin
Release
Sprite demo.exe
Properties
Resources
cartoon grass.jpg
smurf (small).spr
Sprite Editor
Sprite Editor
Sprite Editor.csproj.user
sprite Editor.ico
bin
Release
cLibPicBoxViewer.dll
Sprite Editor.exe
Properties
Resources
click to load image.bmp
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GraphicsEditor_CSharp
{
    public struct udtQ
    {
        public classPixelPos head;
        public classPixelPos tail;
    }

    public enum enuMode { FloodFill, Editor, Pixilate, SpriteBuilder, Straighten }

    public enum enuDirections { north = 0, east, south, west, _numDir }

    public enum enuStraightenMode { setPt_A, setPt_B }

    public partial class formGraphicsEditor : Form
    {
        public enuStraightenMode Straighten_mode = enuStraightenMode.setPt_A;
        public enuMode mode = enuMode.Editor;

        classMath cLibMath = new classMath();

        public formBuildObject frmBuildObject = null;

        Timer tmrReDrawSprite = new Timer();

        classGraphicsPicEditor picEditor;
        bool bolInit = false;

        Point ptClick = new Point();

        // flood fill variables 
        Color clrFloodFill_set = Color.Black;
        Color clrFloodFill_border = Color.White;

        public formGraphicsEditor()
        {
            InitializeComponent();
            picEditor = new classGraphicsPicEditor(this);
            Controls.Add(picEditor);
            picEditor.Visible = true;
            picEditor.pic.Visible = true;
            picEditor.Dock = DockStyle.Fill;

            tmrReDrawSprite.Interval = 100;

            //ToolStripMenuItem mnuFile_LaunchSpriteEditor = new ToolStripMenuItem("&Launch Sprite Editor", null, new EventHandler(mnuFile_LaunchSpriteEditor_Click));
            //mnuFile_LaunchSpriteEditor.Visible = true;
            //picEditor.mnuStrip.Items.Add(mnuFile_LaunchSpriteEditor);

            Activated += new EventHandler(formGraphicsEditor_Activated);
        }

        //void mnuFile_LaunchSpriteEditor_Click(object sender, EventArgs e)
        //{
        //    if (frmBuildObject == null)
        //    {
        //        frmBuildObject = new formBuildObject();
        //        frmBuildObject.Disposed += new EventHandler(frmBuildObject_Disposed);
        //    }
        //    try
        //    {
        //        frmBuildObject.Show();
        //    }
        //    catch (Exception)
        //    {
        //        frmBuildObject = new formBuildObject();
        //        frmBuildObject.Show();
        //    }
        //}

        //void frmBuildObject_Disposed(object sender, EventArgs e) { frmBuildObject = new formBuildObject(); }

        void formGraphicsEditor_Activated(object sender, EventArgs e)
        {            
            if (!bolInit)
            {
                Top = 0; Left = 0;
                Size = Screen.PrimaryScreen.WorkingArea.Size;
                bolInit = true;
            }
        }

        private void mnuColors_Set_Border_Click(object sender, EventArgs e)
        {
            ColorDialog cd = new ColorDialog();
            if (cd.ShowDialog() == DialogResult.OK)
            {
                clrFloodFill_border = cd.Color;
            }
        }

        private void mnuColors_Set_Fill_Click(object sender, EventArgs e)
        {
            ColorDialog cd = new ColorDialog();
            if (cd.ShowDialog() == DialogResult.OK)
            {
                clrFloodFill_set = cd.Color;
            }
        }

        //private void mnuShrink_Click(object sender, EventArgs e)
        //{
        //    Bitmap bmpSource = new Bitmap(picEditor.pic.Image.Width, picEditor.pic.Image.Height);
        //    Bitmap bmpDestination = new Bitmap(bmpSource.Width, bmpSource.Height);
        //    int intPix_X = bmpSource.Width / intPixelSize;
        //    int intPix_Y = bmpSource.Height / intPixelSize;
        //    int intMin = (int)(((double)(intPixelSize * intPixelSize)) / 2.0);

        //    Color clrTestBunker = bmpSource.GetPixel(0, 0);
        //    Color clrTestClear = bmpSource.GetPixel(1, 1);
        //    Color clrTestSpiderWeb = bmpSource.GetPixel(2, 2);
        //    Color clrTestWall = bmpSource.GetPixel(3, 3);

        //    Color clrSetBunker = Color.Gray;
        //    Color clrSetClear = clrTestClear;
        //    Color clrSetSpiderWeb = Color.Brown;
        //    Color clrSetWall = clrTestWall;

        //    int intOffset = intPixelSize / 2;

        //    bmpDestination = new Bitmap(bmpSource.Width / intPixelSize, bmpSource.Height / intPixelSize);

        //    using (Graphics g = Graphics.FromImage(bmpDestination))
        //    {
        //        for (int intX = 0; intX < intPix_X; intX++)
        //            for (int intY = 0; intY < intPix_Y; intY++)
        //            {
        //                Point pt = new Point(intX * intPixelSize, intY * intPixelSize);
        //                Color clrTest = bmpSource.GetPixel(pt.X + intOffset, pt.Y + intOffset);
        //                Color clrSet = clrTestClear;

        //                if (clrTest == clrTestBunker)
        //                    clrSet = clrSetBunker;
        //                else if (clrTest == clrTestSpiderWeb)
        //                    clrSet = clrSetSpiderWeb;
        //                else if (clrTest == clrTestWall)
        //                    clrSet = clrSetWall;
        //                bmpDestination.SetPixel(intX, intY, clrSet);
        //            }
        //    }
        //    picEditor.pic.Image = bmpDestination;
        //}

        private void mnuMode_BuildSprite_Click(object sender, EventArgs e)
        {
            //setMode(enuMode.SpriteBuilder);
            //if (frmBuildObject == null || frmBuildObject.bolDisposed)
            //    frmBuildObject = new formBuildObject();
            //frmBuildObject.Show();

        }
    }

    public class classPixelPos
    {
        public Point pos;
        public classPixelPos next;
    }

    public class classGraphicsPicEditor : Panel
    {
        static bool bolMainDeclared = false;

        #region "enumerated types"
        public enum enuCursorTypes
        {
            arrow,
            cross,
            resizeUpDown,
            resizeLeftRight,
            resizeTLBR,
            resizeBLTR,
            move,
            pen,
            floodfill,
            brush,
            redo,
            undo, 
            eraser,
            line,
            pick_color,
            straighten
        }

        public enum enuMode
        {
            Select,
            Free_Form_Select,
            Eraser,
            Magnify,
            Pick_Color,
            Pencil,
            Flood_Fill,
            Brush,
            Line,
            Straighten,
            undo,
            redo,
            _numModes,
            alt_Zoom
        }

        public enum enuFloodFillMode { toBorder, sameColor }

        public enum enuSelectMode
        {
            settingPivot,
            settingDiagonal,
            hold,
            resizeImage
        }

        public enum enuLineMode
        {
            setPivot,
            setDiagonal
        }

        public enum enuSelectHoldMode
        {
            ignore,
            move,
            resizeTL,
            resizeT,
            resizeTR,
            resizeR,
            resizeBR,
            resizeB,
            resizeBL,
            resizeL
        }

        public enum enuCorner { TL, BL, TR, BR }

        public enum enuHelp
        {
            Select_Mode,
            Free_Form_Select_Mode,
            Eraser_Mode,
            Magnify_Mode,
            Pick_Color_Mode,
            Pencil_Mode,
            Flood_Fill_Mode,
            Brush_Mode,
            Line_Mode,
            Straighten_Mode,
            undo_Mode,
            redo_Mode,
            _NumTypeHelp
        }

        #endregion

        #region "structures"
        public struct udtDrawLineData
        {
            public Point ptPivot;
            public enuLineMode mode;
        }

        public struct udtPixilateColorFind
        {
            public Color clr;
            public int numFound;
        }

        public struct udtCorners
        {
            public Point TL;
            public Point BR;
            public Point BL;
            public Point TR;
        }

        public struct udtSelectHold
        {
            public enuSelectHoldMode mode;
            public Point ptMouseGrabLoc;
            public Point ptNewLoc;
            public bool active;
            public bool drawOpaque;
        }

        public struct udtSelect
        {
            public Point ptPivot;
            public Point ptDiagonal;

            public enuCorner moving;
            public enuCorner setting;

            public udtCorners corners;

            public bool bolEraseBack;

            public enuSelectMode mode;
            public udtSelectHold hold;
            public udtBMPselect bmp;
            public udtBMPselect bmpZoomed;
        }

        public struct udtBMPselect
        {
            public Bitmap bmpBackground;
            public Bitmap bmpSelected;
            public Bitmap bmpRememberSelected;
        }
        #endregion

        #region "objects"
        public System.Windows.Forms.Form callingForm;
        public PictureBox pic = new PictureBox();
        formFlipRotate frmFlipRotate;

        public MenuStrip mnuStrip = new MenuStrip();
        ToolStripMenuItem mnuFile;
        ToolStripMenuItem mnuFile_New;
        ToolStripMenuItem mnuFile_NewWindow;
        ToolStripMenuItem mnuFile_Save;
        ToolStripMenuItem mnuFile_Load;
        ToolStripMenuItem mnuFile_Resize;
        ToolStripMenuItem mnuFile_Exit;
        ToolStripMenuItem mnuEdit;
        ToolStripMenuItem mnuEdit_Undo;
        ToolStripMenuItem mnuEdit_Redo;
        ToolStripMenuItem mnuEdit_Cut;
        ToolStripMenuItem mnuEdit_Paste;
        ToolStripMenuItem mnuEdit_Copy;
        ToolStripMenuItem mnuEdit_SelectAll;
        ToolStripMenuItem mnuImage;
        ToolStripMenuItem mnuImage_Attributes;
        ToolStripMenuItem mnuImage_FlipRotate;
        ToolStripMenuItem mnuImage_DrawOpaque;

        Panel pnlZoom = new Panel();
        Panel pnlFloodFill = new Panel();
        Panel pnlModeSelect = new Panel();
        Panel pnlColorSelect = new Panel();

        Label lblFloodFill_changemode = new Label();

        Label lblHelp = new Label();
        Label lblFeedback = new Label();

        VScrollBar vsb = new VScrollBar();
        HScrollBar hsb = new HScrollBar();

        ProgressBar progressBar = new ProgressBar();

        classMath cLibMath = new classMath();

        Timer tmrHelp_Show = new Timer();
        Timer tmrHelp_Hide = new Timer();

        Timer tmrFeedback_Hide = new Timer();
        #endregion

        #region "variables"
        static int intMyID_Counter = 0;
        public int intMyID;

        const string BackUpFilename = "images";
        const string BackUpExtension = ".bk";
        const int conVerticalOffset = 35;

        int intBackUpFileID_current = 0;
        int intBackUpFileID_Max = 0;

        Point ptMouse;
        Point ptOldMouse;

        Point ptAltZoom_TL;
        Size szMaxAltZoomView;

        enuMode mode = enuMode.Pencil;
        enuMode mode_NextSelection = enuMode._numModes;
        enuMode modeRemember = enuMode.Pencil;

        enuHelp HelpType = enuHelp._NumTypeHelp;

        public Bitmap bmp;
        Bitmap bmpZoomed;
        public Bitmap bmpClipBoard;
        public Bitmap bmpSelected;
        public bool bolMouseDown = false;

        Color clr = Color.Black;
        bool bolIAmMain = false;
        Point ptClrSelect = new Point();
        Point ptModeSelect = new Point();
        Size szModeImage;
        int intZoomFactor = 1;

        enuCursorTypes cutCurrentCursorType = enuCursorTypes.pen;
        Cursor curPic;

        // pixilate variables
        int intPixelSize = 5;

        // floodfill
        public enuFloodFillMode FloodFill_mode = enuFloodFillMode.sameColor;
        Color clrFloodFill_set = Color.Red;
        Color clrFloodFill_border = Color.Black;
        udtQ picQ = new udtQ();

        // select 
        udtSelect udrSelect;

        // Straighten
        enuStraightenMode StraightenMode;
        Point ptStraighten_A;
        Point ptStraighten_B;

        // draw line
        udtDrawLineData udrDrawLine;

        // pencil
        bool bolPencilMoved = false;

        // debug 
        //int intCounter = 0;
        #endregion

        public classGraphicsPicEditor(System.Windows.Forms.Form CallingForm)
        {
            callingForm = CallingForm;

            intMyID = intMyID_Counter++;

            // set static variables
            bolIAmMain = !bolMainDeclared;
            bolMainDeclared = true;

            // init variables
            szModeImage = ModeImageSize();

            #region "add objects & their eventhandlers to panel"
            // add objects to panel
            Controls.Add(pnlColorSelect);
            pnlColorSelect.BorderStyle = BorderStyle.FixedSingle;
            pnlColorSelect.Click += new EventHandler(pnlColorSelect_Click);
            pnlColorSelect.MouseMove += new MouseEventHandler(pnlColorSelect_MouseMove);

            Controls.Add(pnlModeSelect);
            pnlModeSelect.BorderStyle = BorderStyle.FixedSingle;
            pnlModeSelect.BackColor = Color.White;
            pnlModeSelect.MouseMove += new MouseEventHandler(pnlModeSelect_MouseMove);
            pnlModeSelect.MouseClick += new MouseEventHandler(pnlModeSelect_MouseClick);
            pnlModeSelect.MouseLeave += new EventHandler(pnlModeSelect_MouseLeave);

            Controls.Add(pnlFloodFill);
            pnlFloodFill.BackColor = Color.White;
            pnlFloodFill.Height = GraphicsEditor_CSharp.Properties.Resources.FloodFill_pnlBackground_sameColor.Height;
            pnlFloodFill.Width = GraphicsEditor_CSharp.Properties.Resources.FloodFill_pnlBackground_sameColor.Width;
            drawFloodFillDisplayPanel(); pnlFloodFill.Visible = false;
            pnlFloodFill.MouseClick += new MouseEventHandler(pnlFloodFill_MouseClick);

            Controls.Add(lblFloodFill_changemode);
            lblFloodFill_changemode.AutoSize = true;
            lblFloodFill_changemode.MouseEnter += new EventHandler(lbl_MouseEnter);
            lblFloodFill_changemode.MouseLeave += new EventHandler(lbl_MouseLeave);
            lblFloodFill_changemode.MouseClick += new MouseEventHandler(lblFloodFill_changemode_MouseClick);

            Controls.Add(vsb);
            vsb.ValueChanged += new EventHandler(vsb_ValueChanged);
            vsb.Maximum = 1000; vsb.Minimum = 0;

            Controls.Add(hsb);
            hsb.ValueChanged += new EventHandler(hsb_ValueChanged);
            hsb.Maximum = 1000; hsb.Minimum = 0;

            Controls.Add(pic);
            pic.SizeMode = PictureBoxSizeMode.Normal;
            pic.BorderStyle = BorderStyle.Fixed3D;
            pic.BackColor = Color.Gray;
            pic.SendToBack();
            pic.MouseMove += new MouseEventHandler(pic_MouseMove);
            pic.Click += new EventHandler(pic_Click);
            pic.LostFocus += new EventHandler(pic_LostFocus);
            pic.MouseEnter += new EventHandler(pic_MouseEnter);
            pic.MouseLeave += new EventHandler(pic_MouseLeave);
            pic.MouseDown += new MouseEventHandler(pic_MouseDown);
            pic.MouseUp += new MouseEventHandler(pic_MouseUp);

            Controls.Add(lblFeedback);
            lblFeedback.AutoSize = true;
            lblFeedback.Visible = false;
            lblFeedback.BringToFront();

            Controls.Add(lblHelp);
            lblHelp.AutoSize = true;
            lblHelp.Visible = false;
            lblHelp.BringToFront();

            Controls.Add(progressBar);
            progressBar.Visible = false;
            progressBar.BringToFront();

            Controls.Add(mnuStrip);
            mnuStrip.Dock = DockStyle.Top;
            mnuStrip.Visible = true;

            tmrHelp_Show.Interval = 500;
            tmrHelp_Show.Tick += new EventHandler(tmrHelp_Show_Tick);

            tmrHelp_Hide.Interval = 2500;
            tmrHelp_Hide.Tick += new EventHandler(tmrHelp_Hide_Tick);

            tmrFeedback_Hide.Interval = 2500;
            tmrFeedback_Hide.Tick += new EventHandler(tmrFeedback_Hide_Tick);
            #endregion

            #region "menus"
            //  file
            mnuFile = new ToolStripMenuItem("&File");
            mnuStrip.Items.Add(mnuFile);
            mnuFile.Visible = true;

            mnuFile_New = new ToolStripMenuItem("&New", null, new EventHandler(mnuFile_New_Click));
            mnuFile_New.Visible = true;
            mnuFile.DropDownItems.Add(mnuFile_New);

            mnuFile_NewWindow = new ToolStripMenuItem("New&Window", null, new EventHandler(mnuFile_NewWindow_Click));
            mnuFile_NewWindow.Visible = true;
            mnuFile.DropDownItems.Add(mnuFile_NewWindow);

            mnuFile_Save = new ToolStripMenuItem("&Save", null, new EventHandler(mnuFile_Save_Click));
            mnuFile_Save.Visible = true;
            mnuFile.DropDownItems.Add(mnuFile_Save);

            mnuFile_Load = new ToolStripMenuItem("&Load", null, new EventHandler(mnuFile_Load_Click));
            mnuFile_Load.Visible = true;
            mnuFile.DropDownItems.Add(mnuFile_Load);

            mnuFile_Resize = new ToolStripMenuItem("&Resize", null, new EventHandler(mnuFile_Resize_Click));
            mnuFile_Resize.Visible = true;
            mnuFile.DropDownItems.Add(mnuFile_Resize);

            mnuFile_Exit = new ToolStripMenuItem("E&xit", null, new EventHandler(mnuFile_Exit_Click));
            mnuFile_Exit.Visible = true;
            mnuFile.DropDownItems.Add(mnuFile_Exit);

            // edit
            mnuEdit = new ToolStripMenuItem("&Edit");
            mnuStrip.Items.Add(mnuEdit);
            mnuEdit.Visible = true;

            mnuEdit_Undo = new ToolStripMenuItem("&Undo", null, new EventHandler(mnuEdit_Undo_Click), Keys.Control | Keys.Z);
            mnuEdit.DropDownItems.Add(mnuEdit_Undo);
            mnuEdit_Undo.Visible = true;

            mnuEdit_Redo = new ToolStripMenuItem("&Redo", null, new EventHandler(mnuEdit_Redo_Click), Keys.Control | Keys.Y);
            mnuEdit.DropDownItems.Add(mnuEdit_Redo);
            mnuEdit_Redo.Visible = true;

            mnuEdit_Cut = new ToolStripMenuItem("&Cut", null, new EventHandler(mnuEdit_Cut_Click), Keys.Control | Keys.X);
            mnuEdit.DropDownItems.Add(mnuEdit_Cut);
            mnuEdit_Cut.Visible = true;

            mnuEdit_Paste = new ToolStripMenuItem("&Paste", null, new EventHandler(mnuEdit_Paste_Click), Keys.Control | Keys.V);
            mnuEdit.DropDownItems.Add(mnuEdit_Paste);
            mnuEdit_Paste.Visible = true;

            mnuEdit_Copy = new ToolStripMenuItem("&Copy", null, new EventHandler(mnuEdit_Copy_Click), Keys.Control | Keys.C);
            mnuEdit.DropDownItems.Add(mnuEdit_Copy);
            mnuEdit_Copy.Visible = true;

            mnuEdit_SelectAll = new ToolStripMenuItem("Select &All", null, new EventHandler(mnuEdit_SelectAll_Click), Keys.Control | Keys.A);
            mnuEdit.DropDownItems.Add(mnuEdit_SelectAll);
            mnuEdit_SelectAll.Visible = true;

            // IMAGE
            mnuImage = new ToolStripMenuItem("&Image", null, new EventHandler(mnuImage_click));
            mnuStrip.Items.Add(mnuImage);
            mnuImage.Visible = true;

            mnuImage_Attributes = new ToolStripMenuItem("&Attributes", null, new EventHandler(mnuImage_Attributes_Click));
            mnuImage.DropDownItems.Add(mnuImage_Attributes);
            mnuImage_Attributes.Visible = true;

            mnuImage_FlipRotate = new ToolStripMenuItem("&FlipRotate", null, new EventHandler(mnuImage_FlipRotate_Click));
            mnuImage.DropDownItems.Add(mnuImage_FlipRotate);
            mnuImage_FlipRotate.Visible = true;

            mnuImage_DrawOpaque = new ToolStripMenuItem("&DrawOpaque", null, new EventHandler(mnuImage_DrawOpaque_Click));
            udrSelect.hold.drawOpaque = mnuImage_DrawOpaque.Checked = true;            
            mnuImage_DrawOpaque.CheckOnClick = true;
            mnuImage.DropDownItems.Add(mnuImage_DrawOpaque);
            mnuImage_DrawOpaque.Visible = true;

            mnuStrip.BringToFront();

            #endregion

            bmp = new Bitmap(500, 500);
            setZoomFactor(1);
            bmpZoomed = new Bitmap(bmp.Width, bmp.Height);

            // panel event handlers
            SizeChanged += new EventHandler(classGraphicsPicEditor_SizeChanged);
            Disposed += new EventHandler(classGraphicsPicEditor_Disposed);
            MouseEnter += new EventHandler(classGraphicsPicEditor_MouseEnter);
            MouseClick += new MouseEventHandler(classGraphicsPicEditor_MouseClick);

            mnuFile_New_Click((object)null, new EventArgs());

            placeObjectsOnPanel();
            resizeImage();
            setMode(enuMode.Pencil);
        }
        
        void classGraphicsPicEditor_MouseClick(object sender, MouseEventArgs e) { unselect(); }

        void classGraphicsPicEditor_MouseEnter(object sender, EventArgs e) { hideModeObjects(); }

        void lbl_MouseLeave(object sender, EventArgs e)
        {
            Label thisLabel = (Label)sender;
            thisLabel.BackColor = Color.White;
            thisLabel.ForeColor = Color.Black;
        }

        void lbl_MouseEnter(object sender, EventArgs e)
        {
            Label thisLabel = (Label)sender;
            thisLabel.BackColor = Color.Black;
            thisLabel.ForeColor = Color.White;
        }

        void tmrHelp_Hide_Tick(object sender, EventArgs e) { tmrHelp_Hide.Enabled = false; hideHelp(); }
        void tmrHelp_Show_Tick(object sender, EventArgs e)
        {
            tmrHelp_Show.Enabled = false;
            if ((int)HelpType < (int)enuMode._numModes)
            {
                lblHelp.Text = HelpType.ToString().Replace("_", " ").Replace("Mode", "").Trim();
                switch (HelpType)
                {
                    case enuHelp.Brush_Mode:
                    case enuHelp.Eraser_Mode:
                    case enuHelp.Free_Form_Select_Mode:
                        lblHelp.Text += "-NOT IMPLEMENTED YET-";
                        break;
                }
                lblHelp.Left = pnlModeSelect.Left + pnlModeSelect.Width;
                lblHelp.Top = pnlModeSelect.Top + (int)mode_NextSelection * GraphicsEditor_CSharp.Properties.Resources.Brush_Icon.Height;
                lblHelp.Visible = true;
            }
            else
                switch (HelpType)
                {
                    case enuHelp.Flood_Fill_Mode:
                        break;
                }
            lblHelp.BringToFront();
            tmrHelp_Hide.Enabled = true;
        }

        void ResetHelpTimer()
        {
            tmrHelp_Show.Enabled = false;
            tmrHelp_Show.Enabled = true;
            hideHelp();
            //intCounter++;
            //callingForm.Text = intCounter.ToString();
        }

        void KillHelpTimer() { tmrHelp_Show.Enabled = false; }
        void hideHelp() { lblHelp.Visible = false; }

        void classGraphicsPicEditor_Disposed(object sender, EventArgs e)
        {
            string strWorkingDirectory = System.IO.Directory.GetCurrentDirectory();
            string[] strFile = System.IO.Directory.GetFiles(strWorkingDirectory, BackUpFilename + intMyID_Counter.ToString("d4") + "*" + BackUpExtension);
            for (int intFileCounter = 0; intFileCounter < strFile.Length; intFileCounter++)
                System.IO.File.Delete(strFile[intFileCounter]);
            callingForm.Dispose();
        }

        void setMode(enuMode modNew)
        {
            IntPtr ptr;
            unselect();
            mode = modNew;
            switch (mode)
            {
                case enuMode.Select:
                    udrSelect.mode = enuSelectMode.settingPivot;
                    setCursor(enuCursorTypes.cross);
                    modeRemember = mode;
                    break;

                case enuMode.Flood_Fill:
                    setCursor(enuCursorTypes.floodfill);
                    modeRemember = mode;
                    modeRemember = mode;
                    break;

                case enuMode.Pick_Color:
                    setCursor(enuCursorTypes.pick_color);
                    break;

                case enuMode.Pencil:
                    setCursor(enuCursorTypes.pen);
                    modeRemember = mode;
                    break;

                case enuMode.Line:
                    udrDrawLine.mode = enuLineMode.setPivot;
                    setCursor(enuCursorTypes.cross);
                    modeRemember = mode;
                    break;

                default:
                    setCursor(enuCursorTypes.arrow);
                    break;
            }
        }

        void setCursor(enuCursorTypes cutNewCursor)
        {
            if (cutNewCursor == cutCurrentCursorType)
                return;
            cutCurrentCursorType = cutNewCursor;

            IntPtr ptr;

            switch (cutNewCursor)
            {
                case enuCursorTypes.cross:
                    Bitmap bmpCross = new Bitmap(GraphicsEditor_CSharp.Properties.Resources.cursor_cross);
                    bmpCross.MakeTransparent();
                    try
                    {
                        ptr = bmpCross.GetHicon();
                        curPic = new Cursor(ptr);
                    }
                    catch (Exception) { }
                    return;

                case enuCursorTypes.floodfill:
                    Bitmap bmpFloodFillIcon = new Bitmap(GraphicsEditor_CSharp.Properties.Resources.FloodFill_Icon);
                    bmpFloodFillIcon.MakeTransparent();
                    ptr = bmpFloodFillIcon.GetHicon();
                    curPic = new Cursor(ptr);
                    return;


                case enuCursorTypes.pen:
                    Bitmap bmpPencil = new Bitmap(GraphicsEditor_CSharp.Properties.Resources.Pencil_Icon);
                    bmpPencil.MakeTransparent();
                    ptr = bmpPencil.GetHicon();
                    curPic = new Cursor(ptr);
                    return;

                case enuCursorTypes.pick_color:
                    Bitmap bmpEyeDropper = new Bitmap(GraphicsEditor_CSharp.Properties.Resources.Pick_color_Icon);
                    bmpEyeDropper.MakeTransparent();
                    ptr = bmpEyeDropper.GetHicon();
                    curPic = new Cursor(ptr);
                    return;

                case enuCursorTypes.resizeTLBR:
                    Bitmap bmpResizeTLBR = new Bitmap(GraphicsEditor_CSharp.Properties.Resources.cursor_resizeDiagonal_TL_BR);
                    bmpResizeTLBR.MakeTransparent();
                    try
                    {
                        ptr = bmpResizeTLBR.GetHicon();
                        curPic = new Cursor(ptr);
                    }
                    catch (Exception) { }
                    return;

                case enuCursorTypes.resizeBLTR:
                    Bitmap resizeBLTR = new Bitmap(GraphicsEditor_CSharp.Properties.Resources.cursor_resizeDiagonal_BL_TR);
                    resizeBLTR.MakeTransparent();
                    try
                    {
                        ptr = resizeBLTR.GetHicon();
                        curPic = new Cursor(ptr);
                    }
                    catch (Exception) { }
                    return;

                case enuCursorTypes.resizeLeftRight:
                    Bitmap bmpResizeLR= new Bitmap(GraphicsEditor_CSharp.Properties.Resources.cursor_resizeLeftRight);
                    bmpResizeLR.MakeTransparent();
                    try
                    {
                        ptr = bmpResizeLR.GetHicon();
                        curPic = new Cursor(ptr);
                    }
                    catch (Exception) { }
                    return;

                case enuCursorTypes.resizeUpDown:
                    Bitmap bmpResizeUD = new Bitmap(GraphicsEditor_CSharp.Properties.Resources.cursor_resizeUpDown);
                    bmpResizeUD.MakeTransparent();
                    try
                    {
                        ptr = bmpResizeUD.GetHicon();
                        curPic = new Cursor(ptr);
                    }
                    catch (Exception) { }
                    return;

                case enuCursorTypes.move:
                    Bitmap bmpMove= new Bitmap(GraphicsEditor_CSharp.Properties.Resources.cursor_move);
                    bmpMove.MakeTransparent();
                    ptr = bmpMove.GetHicon();
                    curPic = new Cursor(ptr);
                    return;

                default:
                    curPic = Cursors.Arrow;
                    return;
            }
        }

        void pic_MouseUp(object sender, MouseEventArgs e)
        {
            bolMouseDown = false;
            if (mode == enuMode.Pencil)
                backupImage();
        }
        void pic_MouseDown(object sender, MouseEventArgs e) { bolMouseDown = true; }
        void pic_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
            bolMouseDown = false;
            if (mode == enuMode.Pencil)
                backupImage();
        }
        void pic_MouseEnter(object sender, EventArgs e) { Cursor = curPic; bolMouseDown = false; hideModeObjects(); }

        void feedback(string strMessage) { feedback(strMessage, lblFeedback.Location, lblFeedback.ForeColor, lblFeedback.BackColor, lblFeedback.Font); }
        void feedback(string strMessage, Point pt) { feedback(strMessage, pt, lblFeedback.ForeColor, lblFeedback.BackColor, lblFeedback.Font); }
        void feedback(string strMessage, Point pt, Color clrForeground, Color clrBackground) { feedback(strMessage, pt, clrForeground, clrBackground, lblFeedback.Font); }
        void feedback(string strMessage, Point pt, Color clrForeground, Color clrBackground, Font thisFont)
        {
            lblFeedback.Location = pt;
            lblFeedback.Text = strMessage;
            lblFeedback.ForeColor = clrForeground;
            lblFeedback.BackColor = clrBackground;
            lblFeedback.Font = thisFont;
            lblFeedback.BringToFront();
            lblFeedback.Visible = true;
            tmrFeedback_Hide.Enabled = true;
        }
        void tmrFeedback_Hide_Tick(object sender, EventArgs e) { tmrFeedback_Hide.Enabled = lblFeedback.Visible = false; }

        void pic_Click(object sender, EventArgs e)
        {
            switch (mode)
            {
                case enuMode.Select:
                    switch (udrSelect.mode)
                    {
                        case enuSelectMode.resizeImage:
                            Bitmap bmpNew = new Bitmap(pic.Image.Width, pic.Image.Height);
                            using (Graphics g = Graphics.FromImage(bmpNew))
                            {
                                g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, bmpNew.Width, bmpNew.Height));
                                g.DrawImage(bmp, new Point(0, 0));
                            }
                            bmp = new Bitmap(bmpNew);
                            resizeImage();

                            udrSelect.mode = enuSelectMode.settingPivot;
                            break;

                        case enuSelectMode.settingPivot:
                            if (intZoomFactor ==1
                                && mode ==  enuMode.Select 
                                && udrSelect.mode == enuSelectMode.settingPivot 
                                && (Math.Abs(ptMouse.X - pic.Image.Width) < 3 && Math.Abs(ptMouse.Y - pic.Image.Height) < 3))
                            {
                                udrSelect.mode = enuSelectMode.resizeImage;
                            }
                            else
                            {
                                udrSelect.hold.ptNewLoc = udrSelect.ptPivot = ptMouse;
                                udrSelect.mode = enuSelectMode.settingDiagonal;
                            }
                            break;

                        case enuSelectMode.settingDiagonal:
                            udrSelect.ptDiagonal = ptMouse;
                            udrSelect.mode = enuSelectMode.hold;
                            udrSelect.hold.active = false;
                            udrSelect.hold.mode = enuSelectHoldMode.ignore;
                            udrSelect.hold.ptNewLoc = udrSelect.corners.TL;
                            eraseSelectedFromBackground();

                            //cLibPicBoxViewer pic2 = new cLibPicBoxViewer(udrSelect.bmp.bmpSelected, "select in pic click");
                            drawSelectedRectangle();
                            drawSelect();
                            break;

                        case enuSelectMode.hold:
                            clickPicInSelectHoldMode();
                            break;
                    }
                    break;

                case enuMode.Pick_Color:
                    try
                    {
                        setClr(bmp.GetPixel(ptMouse.X, ptMouse.Y));
                    }
                    catch (Exception) { }
                    setMode(modeRemember);
                    break;

                case enuMode.Pencil:
                    if (!bolPencilMoved)
                    {
                        try { bmp.SetPixel(ptMouse.X, ptMouse.Y, clr); }
                        catch (Exception) { }
                        if (intZoomFactor > 1)
                        {
                            using (Graphics g = Graphics.FromImage(bmpZoomed))
                            {
                                int intLeft = (ptMouse.X) * intPixelSize + 1;
                                int intTop = (ptMouse.Y) * intPixelSize + 1;
                                g.FillRectangle(new SolidBrush(clr), intLeft, intTop, intPixelSize, intPixelSize);
                            }
                            pic.Image = bmpZoomed;
                        }
                        else
                        {
                            pic.Image = bmp;
                        }
                        pic.Refresh();
                    }
                    bolPencilMoved = false;
                    break;

                case enuMode.Flood_Fill:
                    if (FloodFill_mode == enuFloodFillMode.toBorder)
                        floodfill_toBorder(ptMouse);
                    else
                        floodfill_sameColor(ptMouse);
                    break;

                case enuMode.Straighten:
                    switch (StraightenMode)
                    {
                        case enuStraightenMode.setPt_A:
                            ptStraighten_A = ptMouse;
                            StraightenMode = enuStraightenMode.setPt_B;
                            break;

                        case enuStraightenMode.setPt_B:
                            double dblStraightenAngle = cLibMath.arcTan(ptStraighten_B.X - ptStraighten_A.X, ptStraighten_B.Y - ptStraighten_A.Y);
                            Bitmap bmpTemp = new Bitmap(bmp);
                            bmp = StraightenImage(bmpTemp, dblStraightenAngle);
                            pic.Image = bmp;
                            resizeImage();
                            pic.Refresh();
                            backupImage();
                            setMode(enuMode.Pencil);
                            StraightenMode = enuStraightenMode.setPt_A;
                            break;
                    }
                    break;

                case enuMode.Line:
                    switch (udrDrawLine.mode)
                    {
                        case enuLineMode.setPivot:
                            udrDrawLine.ptPivot = new Point(ptMouse.X, ptMouse.Y);
                            udrDrawLine.mode = enuLineMode.setDiagonal;
                            break;

                        case enuLineMode.setDiagonal:
                            drawLine(udrDrawLine.ptPivot, ptMouse);
                            udrDrawLine.mode = enuLineMode.setPivot;
                            backupImage();
                            break;
                    }
                    break;
            }
        }

        void pic_LostFocus(object sender, EventArgs e)
        {
            if (mode == enuMode.Select)
                if (udrSelect.mode == enuSelectMode.hold)
                    unselect();
        }

        void unselect()
        {
            if ((udrSelect.bmp.bmpSelected == null && intZoomFactor == 1)
                || udrSelect.bmpZoomed.bmpSelected == null && intZoomFactor > 1)
                return;

            using (Graphics g = Graphics.FromImage(bmp))
            {
                Bitmap bmpSelected = new Bitmap(udrSelect.bmp.bmpSelected);
                if (!mnuImage_DrawOpaque.Checked)
                {
                    bmpSelected.MakeTransparent(Color.White);
                }
                g.DrawImage(udrSelect.bmp.bmpBackground, new Point(0, 0));
                g.DrawImage(bmpSelected, udrSelect.hold.ptNewLoc);
            }

            if (intZoomFactor > 1)
            {
                using (Graphics g = Graphics.FromImage(bmpZoomed))
                {
                    Bitmap bmpSelected = new Bitmap(udrSelect.bmpZoomed.bmpSelected);
                    if (!mnuImage_DrawOpaque.Checked)
                    {
                        bmpSelected.MakeTransparent(Color.White);
                    }
                       g.DrawImage(bmpSelected,
                                    new Point((udrSelect.corners.TL.X) * intPixelSize + 1,
                                              (udrSelect.corners.TL.Y) * intPixelSize + 1));




                    //g.DrawImage(udrSelect.bmpZoomed.bmpBackground, new Point(0, 0));
                    //g.DrawImage(bmpSelected, new Point(udrSelect.corners.TL.X, udrSelect.corners.TL.Y));
                    ///

                    //g.DrawImage(udrSelect.bmpZoomed.bmpBackground, new Point(0, 0));
                    //if (!mnuImage_DrawOpaque.Checked)
                    //    udrSelect.bmpZoomed.bmpSelected.MakeTransparent();
                    //g.DrawImage(udrSelect.bmpZoomed.bmpSelected,
                    //            new Point((udrSelect.hold.ptNewLoc.X-1) * intPixelSize +1,
                    //                      (udrSelect.hold.ptNewLoc.Y-1) * intPixelSize+1));
                }
                pic.Image = bmpZoomed;
            }
            else
                pic.Image = bmp;

            pnlFloodFill.Visible = pnlFloodFill.Visible = pnlZoom.Visible = false;
            initSelectVars();
            pic.Refresh();
            backupImage();
        }

        void drawSelectedRectangle()
        {
            if (udrSelect.bmp.bmpSelected == null)
                return;
            Rectangle recSelectSinglePixel;
            Bitmap bmpSP_Temp = new Bitmap(pic.Image);
            int intLineShift = -1;
            if (intZoomFactor > 1)
            {
                //bmpSP_Temp =new Bitmap( udrSelect.bmpZoomed.bmpBackground);
                //if (udrSelect.bmpZoomed.bmpBackground.Width == udrSelect.bmpZoomed.bmpSelected.Width
                //    && udrSelect.bmpZoomed.bmpBackground.Height == udrSelect.bmpZoomed.bmpSelected.Height)
                //    intLineShift = 1;
                recSelectSinglePixel = new Rectangle(new Point((udrSelect.corners.TL.X) * intPixelSize + 1,
                                                               (udrSelect.corners.TL.Y ) * intPixelSize + 1),
                                                               udrSelect.bmpZoomed.bmpSelected.Size);

                using (Graphics g = Graphics.FromImage(bmpSP_Temp))
                {
                    g.DrawRectangle(new Pen(Color.White), recSelectSinglePixel);
                    g.DrawRectangle(new Pen(Color.Black),
                                    new Rectangle(recSelectSinglePixel.Left + intLineShift,
                                                  recSelectSinglePixel.Top + intLineShift,
                                                  recSelectSinglePixel.Width - 2 * intLineShift,
                                                  recSelectSinglePixel.Height - 2 * intLineShift));
                }
            }
            else
            {
                //bmpSP_Temp =new Bitmap( udrSelect.bmp.bmpBackground);
                recSelectSinglePixel = new Rectangle(udrSelect.corners.TL, udrSelect.bmp.bmpSelected.Size);
                //if (udrSelect.bmp.bmpBackground.Width == udrSelect.bmp.bmpSelected.Width
                //    && udrSelect.bmp.bmpBackground.Height == udrSelect.bmp.bmpSelected.Height)
                //    intLineShift = 1;
                using (Graphics g = Graphics.FromImage(bmpSP_Temp))
                {
                    g.DrawRectangle(new Pen(Color.White), recSelectSinglePixel);
                    g.DrawRectangle(new Pen(Color.Black),
                                    new Rectangle(recSelectSinglePixel.Left + intLineShift,
                                                  recSelectSinglePixel.Top + intLineShift,
                                                  recSelectSinglePixel.Width - 2 * intLineShift,
                                                  recSelectSinglePixel.Height - 2 * intLineShift));
                }
            }
            pic.Image = bmpSP_Temp;
            //drawSelect();
            pic.Refresh();
        }

        void clickPicInSelectHoldMode()
        {
            if (udrSelect.hold.active)
            {
                udrSelect.hold.active = false;

                //if (intZoomFactor > 1) // off by one bug which this hacks back into place
                //    udrSelect.corners.TL = new Point(udrSelect.corners.TL.X - 1, udrSelect.corners.TL.Y - 1);

                drawSelect();
                drawSelectedRectangle();
            }
            else
            {
                switch (udrSelect.hold.mode)
                {
                    case enuSelectHoldMode.ignore:
                        // quit select mode
                        unselect();
                        break;

                    default:
                        udrSelect.hold.active = true;
                        // delete selected area from bmp and store in hold.bmp
                        if (udrSelect.bolEraseBack)
                        {
                            using (Graphics g = Graphics.FromImage(udrSelect.bmp.bmpBackground))
                                g.FillRectangle(new SolidBrush(Color.White),
                                                new Rectangle(udrSelect.hold.ptNewLoc,
                                                              udrSelect.bmp.bmpSelected.Size));
                            if (intZoomFactor > 1)
                            {
                                Rectangle recZoomCut = new Rectangle(new Point((udrSelect.hold.ptNewLoc.X ) * intPixelSize + 1,
                                                                              (udrSelect.hold.ptNewLoc.Y ) * intPixelSize + 1),
                                                                     udrSelect.bmpZoomed.bmpSelected.Size);
                                using (Graphics g = Graphics.FromImage(udrSelect.bmpZoomed.bmpBackground))
                                    g.FillRectangle(new SolidBrush(Color.White),
                                                    recZoomCut);
                            }
                            udrSelect.bolEraseBack = false;
                        }

                        udrSelect.hold.ptMouseGrabLoc = new Point(ptMouse.X - udrSelect.hold.ptNewLoc.X, ptMouse.Y - udrSelect.hold.ptNewLoc.Y);
                        
                        //if (intZoomFactor > 1) // off by one bug somewhere which this hacks back into place
                        //    udrSelect.hold.ptMouseGrabLoc = new Point(udrSelect.hold.ptMouseGrabLoc.X - 1, udrSelect.hold.ptMouseGrabLoc.Y - 1);

                        if (udrSelect.hold.mode != enuSelectHoldMode.move)
                            udrSelect.bmp.bmpRememberSelected = new Bitmap(udrSelect.bmp.bmpSelected);
                        break;
                }
            }
        }

        void moveAltZoom_Left()
        {
            Bitmap bmpCurrent = (Bitmap)pic.Image;
            Bitmap bmpNext = new Bitmap(bmpCurrent.Width, bmpCurrent.Height);
            int intMaxX = szMaxAltZoomView.Width < bmp.Width / intPixelSize ? szMaxAltZoomView.Width : bmp.Width / intPixelSize;
            int intMaxY = szMaxAltZoomView.Height < bmp.Height / intPixelSize ? szMaxAltZoomView.Height : bmp.Height / intPixelSize;
            ptAltZoom_TL.X--;
            using (Graphics g = Graphics.FromImage(bmpNext))
            {
                for (int intY = 1; intY < intMaxY; intY++)
                    g.FillRectangle(new SolidBrush(bmp.GetPixel(ptAltZoom_TL.X, ptAltZoom_TL.Y + intY)),
                                    new Rectangle(1, (intY - 1) * intPixelSize + 1, intPixelSize, intPixelSize));
                g.DrawImage(bmpCurrent,
                            new Rectangle(intPixelSize + 1, 1, (szMaxAltZoomView.Width - 1) * intPixelSize, (szMaxAltZoomView.Height) * intPixelSize),
                            new Rectangle(1, 1, (szMaxAltZoomView.Width - 1) * intPixelSize, (szMaxAltZoomView.Height) * intPixelSize),
                            GraphicsUnit.Pixel);
            }
            pic.Image = bmpNext;
            pic.Refresh();
        }

        void moveAltZoom_Right()
        {
            Bitmap bmpCurrent = (Bitmap)pic.Image;
            Bitmap bmpNext = new Bitmap(bmpCurrent.Width, bmpCurrent.Height);
            int intMaxX = szMaxAltZoomView.Width < bmp.Width / intPixelSize ? szMaxAltZoomView.Width : bmp.Width / intPixelSize;
            int intMaxY = szMaxAltZoomView.Height < bmp.Height / intPixelSize ? szMaxAltZoomView.Height : bmp.Height / intPixelSize;
            ptAltZoom_TL.X++;
            using (Graphics g = Graphics.FromImage(bmpNext))
            {
                for (int intY = 1; intY < intMaxY; intY++)
                {
                    Rectangle recPixel = new Rectangle((szMaxAltZoomView.Width - 1) * intPixelSize + 1, (intY - 1) * intPixelSize + 1, intPixelSize, intPixelSize);
                    g.FillRectangle(new SolidBrush(bmp.GetPixel(ptAltZoom_TL.X + szMaxAltZoomView.Width, ptAltZoom_TL.Y + intY)),
                                    recPixel);
                }
                Rectangle recDest = new Rectangle(1, 1, (szMaxAltZoomView.Width - 1) * intPixelSize, (szMaxAltZoomView.Height) * intPixelSize);
                Rectangle recSrc = new Rectangle(intPixelSize + 1, 1, (szMaxAltZoomView.Width - 1) * intPixelSize, (szMaxAltZoomView.Height) * intPixelSize);

                g.DrawImage(bmpCurrent,
                            recDest,
                            recSrc,
                            GraphicsUnit.Pixel);
            }
            pic.Image = bmpNext;
            pic.Refresh();
        }

        void moveAltZoom_Up()
        {
            Bitmap bmpCurrent = (Bitmap)pic.Image;
            Bitmap bmpNext = new Bitmap(bmpCurrent.Width, bmpCurrent.Height);
            int intMaxX = szMaxAltZoomView.Width < bmp.Width / intPixelSize ? szMaxAltZoomView.Width : bmp.Width / intPixelSize;
            int intMaxY = szMaxAltZoomView.Height < bmp.Height / intPixelSize ? szMaxAltZoomView.Height : bmp.Height / intPixelSize;
            ptAltZoom_TL.Y--;
            using (Graphics g = Graphics.FromImage(bmpNext))
            {
                for (int intX = 1; intX < intMaxX; intX++)
                {
                    g.FillRectangle(new SolidBrush(bmp.GetPixel(ptAltZoom_TL.X + intX, ptAltZoom_TL.Y)),
                                    new Rectangle((intX - 1) * intPixelSize + 1, 1, intPixelSize, intPixelSize));
                }
                g.DrawImage(bmpCurrent,
                            new Rectangle(1, intPixelSize + 1, (szMaxAltZoomView.Width) * intPixelSize, (szMaxAltZoomView.Height - 1) * intPixelSize),
                            new Rectangle(1, 1, szMaxAltZoomView.Width * intPixelSize, (szMaxAltZoomView.Height - 1) * intPixelSize),
                            GraphicsUnit.Pixel);
            }
            pic.Image = bmpNext;
            pic.Refresh();
        }

        void moveAltZoom_Down()
        {
            Bitmap bmpCurrent = (Bitmap)pic.Image;
            Bitmap bmpNext = new Bitmap(bmpCurrent.Width, bmpCurrent.Height);
            int intMaxX = szMaxAltZoomView.Width < bmp.Width / intPixelSize ? szMaxAltZoomView.Width : bmp.Width / intPixelSize;
            int intMaxY = szMaxAltZoomView.Height < bmp.Height / intPixelSize ? szMaxAltZoomView.Height : bmp.Height / intPixelSize;
            ptAltZoom_TL.Y++;
            using (Graphics g = Graphics.FromImage(bmpNext))
            {
                for (int intX = 1; intX < intMaxX; intX++)
                {
                    Rectangle recPixel = new Rectangle((intX - 1) * intPixelSize + 1, (szMaxAltZoomView.Height - 1) * intPixelSize, intPixelSize, intPixelSize);
                    g.FillRectangle(new SolidBrush(bmp.GetPixel(ptAltZoom_TL.X + intX, ptAltZoom_TL.Y + szMaxAltZoomView.Height)),
                                    recPixel);
                }
                Rectangle recDest = new Rectangle(1, 1, (szMaxAltZoomView.Width) * intPixelSize, (szMaxAltZoomView.Height - 1) * intPixelSize);
                Rectangle recSrc = new Rectangle(1, intPixelSize + 1, (szMaxAltZoomView.Width) * intPixelSize, (szMaxAltZoomView.Height - 1) * intPixelSize);

                g.DrawImage(bmpCurrent,
                            recDest,
                            recSrc,
                            GraphicsUnit.Pixel);
            }
            pic.Image = bmpNext;
            pic.Refresh();
        }

        void initAltZoom()
        {
            intPixelSize = 23;
            ptAltZoom_TL = new Point(1, 1);
            szMaxAltZoomView = new Size(pic.Width / intPixelSize, pic.Height / intPixelSize);
            Bitmap bmpAltZoom = new Bitmap(pic.Width, pic.Height);
            int intMaxX = szMaxAltZoomView.Width > bmp.Width ? szMaxAltZoomView.Width : bmp.Width;
            int intMaxY = szMaxAltZoomView.Height > bmp.Height ? szMaxAltZoomView.Height : bmp.Height;
            using (Graphics g = Graphics.FromImage(bmpAltZoom))
            {
                for (int intX = 1; intX < intMaxX; intX++)
                    for (int intY = 1; intY < intMaxY; intY++)
                        g.FillRectangle(new SolidBrush(bmp.GetPixel(intX, intY)), intX * intPixelSize + 1, intY * intPixelSize + 1, intPixelSize, intPixelSize);
            }
            pic.Image = bmpAltZoom;
            pic.Refresh();
        }

        void testSelectBit()
        {
            if (udrSelect.bmp.bmpSelected != null)
            {
                Color clrTestColor = Color.FromArgb(0, 0, 0, 0);
                Color clrTestPixel = udrSelect.bmp.bmpSelected.GetPixel(0, udrSelect.bmp.bmpSelected.Height - 1);
                if (clrTestPixel == clrTestColor)
                    callingForm.Text = "selected bmp pixel WHITE";
                else
                    callingForm.Text = "selected bmp pixel -----";
            }
            else
                callingForm.Text = "no selected bmp";

        }

        void pic_MouseMove(object sender, MouseEventArgs e)
        {
            //testSelectBit();
            //cLibPicBoxViewer pic2 = new cLibPicBoxViewer(udrSelect.bmp.bmpSelected, "pic in mousemove");
            Cursor = curPic;
            ptOldMouse = new Point(ptMouse.X, ptMouse.Y);
            if (intZoomFactor == 1)
                ptMouse = new Point(e.X, e.Y);
            else
                ptMouse = new Point((int)Math.Floor(((double)(e.X) / (double)(intPixelSize)) ),
                                    (int)Math.Floor(((double)(e.Y) / (double)(intPixelSize)) ));
            try
            {
                Color clr = bmp.GetPixel(ptMouse.X, ptMouse.Y);
                callingForm.Text = "(" + ptMouse.X.ToString("d3") + ", " + ptMouse.Y.ToString("d3") + ") : clr: (" + clr.A.ToString() + ", " + clr.R.ToString() + ", " + clr.G.ToString() + ", " + clr.B.ToString() + ")";
            }
            catch (Exception) { }
            switch (mode)
            {
                case enuMode.Line:
                    if (udrDrawLine.mode == enuLineMode.setDiagonal )
                        drawLine(udrDrawLine.ptPivot, ptMouse, false);
                    break;

                case enuMode.alt_Zoom:
                    if (e.X < 100 && ptAltZoom_TL.X > 0)
                        moveAltZoom_Left();
                    else if (e.X > pic.Width - 100 && ptAltZoom_TL.X < bmp.Width - szMaxAltZoomView.Width - 1)
                        moveAltZoom_Right();
                    if (e.Y < 100 && ptAltZoom_TL.Y > 0)
                        moveAltZoom_Up();
                    else if (e.Y > pic.Height - 100 && ptAltZoom_TL.Y < bmp.Height - szMaxAltZoomView.Height - 1)
                        moveAltZoom_Down();
                    break;

                case enuMode.Straighten:
                    Bitmap bmpTemp;
                    switch (StraightenMode)
                    {
                        case enuStraightenMode.setPt_A:
                            ptStraighten_A = ptMouse;
                            bmpTemp = new Bitmap(bmp);
                            using (Graphics g = Graphics.FromImage(bmpTemp))
                                g.DrawEllipse(new Pen(Color.Blue), ptStraighten_A.X - 2, ptStraighten_A.Y - 2, 4, 4);
                            pic.Image = bmpTemp;
                            pic.Refresh();
                            break;

                        case enuStraightenMode.setPt_B:
                            ptStraighten_B = ptMouse;
                            bmpTemp = new Bitmap(bmp);
                            using (Graphics g = Graphics.FromImage(bmpTemp))
                            {
                                g.DrawEllipse(new Pen(Color.Blue), ptStraighten_A.X - 2, ptStraighten_A.Y - 2, 4, 4);
                                g.DrawEllipse(new Pen(Color.Blue), ptStraighten_B.X - 2, ptStraighten_B.Y - 2, 4, 4);
                                g.DrawLine(new Pen(Color.Blue), ptStraighten_A, ptStraighten_B);
                            }
                            pic.Image = bmpTemp;
                            pic.Refresh();
                            break;
                    }
                    break;

                case enuMode.Select:
                    picMouseMove_Select();
                    break;

                case enuMode.Pencil:
                    picMouseMove_Pencil();
                    break;
            }
        }

        void drawLine(Point ptStart, Point ptEnd) { drawLine(ptStart, ptEnd, true); }
        void drawLine(Point pt1, Point pt2, bool bolPermanent)
        {
            Point ptTL = new Point(pt1.X < pt2.X ? pt1.X : pt2.X, pt1.Y < pt2.Y ? pt1.Y : pt2.Y);
            Point ptBR = new Point(pt1.X > pt2.X ? pt1.X : pt2.X, pt1.Y > pt2.Y ? pt1.Y : pt2.Y);

            if (pt1.X > pt2.X)
            {
                Point ptTemp = new Point(pt2.X, pt2.Y);
                pt2 = new Point(pt1.X, pt1.Y);
                pt1 = new Point ( ptTemp.X,  ptTemp.Y);
            }

            // traverse line for every X
            // y = mx + b
            // calculate slope of the line
            double m = (double)(pt2.Y - pt1.Y) / (double)(pt2.X - pt1.X);
            // calculate y offset 'b'
            int b = pt2.Y - (int)(m * pt2.X);

            Bitmap bmpTemp = new Bitmap(bmp);
            Bitmap bmpTempZoom = new Bitmap(bmpZoomed);
            for (int intX = pt1.X; intX <= pt2.X; intX++)
            {
                int intLeft = (intX ) * intPixelSize + 1;
                int intY = (int)(m * intX) + b;
                int intTop = (intY ) * intPixelSize + 1;
                if (intY >= ptTL.Y && intY<= ptBR.Y)
                {
                    if (intZoomFactor > 1)
                        using (Graphics g = Graphics.FromImage(bmpTempZoom))
                            g.FillRectangle(new SolidBrush(clr), intLeft, intTop, intPixelSize, intPixelSize);
                    try { bmpTemp.SetPixel(intX, intY, clr); }
                    catch (Exception) { }
                }
            }

            Point ptStart = new Point();
            Point ptEnd = new Point();
            // traverse line for every Y
            // x = (y-b)/m
            if (pt1.Y < pt2.Y)
            {
                ptStart = pt1;
                ptEnd = pt2;
            }
            else
            {
                ptStart = pt2;
                ptEnd = pt1;
            }
            
            for (int intY = ptStart.Y; intY <= ptEnd.Y; intY++)
            {
                int intX = 0;
                if (ptStart.X == ptEnd.X)
                    intX = ptStart.X;
                else
                    if (intY - b != 0)
                        intX = (int)((intY - b) / m);
                    else
                    {
                        intX = ptStart.X;
                    }
                int intTop = (intY ) * intPixelSize + 1;
                int intLeft = (intX) * intPixelSize + 1;
                if (intX >= ptTL.X && intX <= ptBR.X)
                {
                    if (intZoomFactor > 1)
                        using (Graphics g = Graphics.FromImage(bmpTempZoom))
                            g.FillRectangle(new SolidBrush(clr), intLeft, intTop, intPixelSize, intPixelSize);
                    try { bmpTemp.SetPixel(intX, intY, clr); }
                    catch (Exception) { }
                }
            }

            if (bolPermanent)
            {
                bmp = bmpTemp;
                bmpZoomed = bmpTempZoom;
            }

            if (intZoomFactor == 1)
                pic.Image = bmpTemp;
            else
                pic.Image = bmpTempZoom;

            pic.Refresh();
        }

        void picMouseMove_Pencil()
        {
            if (bolMouseDown)
            {
                bolPencilMoved = true;
                ptMouse = new Point(ptMouse.X + 1, ptMouse.Y + 1);
                if (Math.Abs(ptMouse.X - ptOldMouse.X) > 1
                    || Math.Abs(ptMouse.Y - ptOldMouse.Y) > 1)
                {
                   
                    Point ptStart, ptEnd;
                    if (ptMouse.X < ptOldMouse.X)
                    {
                        ptStart = ptMouse;
                        ptEnd = ptOldMouse;
                    }
                    else
                    {
                        ptStart = ptOldMouse;
                        ptEnd = ptMouse;
                    }

                    drawLine(ptStart, ptEnd);
                }
                else
                {
                    try
                    {
                        try { bmp.SetPixel(ptMouse.X-1, ptMouse.Y-1, clr); }
                        catch (Exception) { }
                        if (intZoomFactor > 1)
                        {
                            using (Graphics g = Graphics.FromImage(bmpZoomed))
                            {
                                int intLeft = (ptMouse.X - 1) * intPixelSize + 1;
                                int intTop = (ptMouse.Y - 1) * intPixelSize + 1;
                                g.FillRectangle(new SolidBrush(clr), intLeft, intTop, intPixelSize, intPixelSize);
                            }
                            pic.Image = bmpZoomed;
                        }
                        else
                            pic.Image = bmp;
                        pic.Refresh();
                    }
                    catch (Exception) { }
                }
                //pic.Image = bmp;
            }
        }

        void drawSelect()
        {
            if (udrSelect.bmp.bmpSelected == null )
                return;

           Bitmap bmpTemp;
            if (intZoomFactor > 1)
            {
                bmpTemp = new Bitmap(udrSelect.bmpZoomed.bmpBackground);
                Bitmap bmpSelect = new Bitmap(udrSelect.bmpZoomed.bmpSelected);

                if (!udrSelect.hold.drawOpaque)
                    bmpSelect.MakeTransparent(Color.White);
                
                using (Graphics g = Graphics.FromImage(bmpTemp))
                    g.DrawImage(bmpSelect,
                                new Point((udrSelect.corners.TL.X) * intPixelSize +1,
                                          (udrSelect.corners.TL.Y) * intPixelSize+1));
            }
            else
            {
                bmpTemp = new Bitmap(udrSelect.bmp.bmpBackground);
                
                Bitmap bmpSelect = new Bitmap(udrSelect.bmp.bmpSelected);

                if (!udrSelect.hold.drawOpaque)
                    bmpSelect.MakeTransparent(Color.White);
              
                using (Graphics g = Graphics.FromImage(bmpTemp))
                    g.DrawImage(bmpSelect, udrSelect.corners.TL);
            }
            pic.Image = bmpTemp;
            //cLibPicBoxViewer pic2 = new cLibPicBoxViewer(pic.Image, "bmp drawselect");
            pic.Refresh();
        }

        void picMouseMove_Select()
        {
            switch (udrSelect.mode)
            {
                case enuSelectMode.settingPivot:
                    if (mode == enuMode.Select
                                && udrSelect.mode == enuSelectMode.settingPivot
                                && intZoomFactor ==1
                                && (Math.Abs(ptMouse.X - pic.Image.Width) < 3 && Math.Abs(ptMouse.Y - pic.Image.Height) < 3))
                    {
                        setCursor(enuCursorTypes.resizeTLBR);
                    }
                    else
                    {
                        setCursor(enuCursorTypes.cross);
                    }
                    break;

                case enuSelectMode.resizeImage:
                    Bitmap bmpNewImage = new Bitmap(ptMouse.X, ptMouse.Y);
                    using (Graphics g = Graphics.FromImage(bmpNewImage))
                    {
                        g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, bmpNewImage.Width, bmpNewImage.Height));
                        g.DrawImage(bmp, new Point(0,0));
                    }
                    pic.Image = new Bitmap(bmpNewImage);
                    pic.Width = pic.Image.Width + 10; pic.Height = pic.Image.Height + 10;
                    break;

                case enuSelectMode.settingDiagonal:
                    udrSelect.ptDiagonal = ptMouse;
                    if (udrSelect.ptDiagonal.X == udrSelect.ptPivot.X
                        || udrSelect.ptDiagonal.Y == udrSelect.ptPivot.Y)
                    {
                        if (intZoomFactor > 1)
                        {
                            Rectangle recSelectSinglePixel = new Rectangle(new Point((udrSelect.ptPivot.X ) * intPixelSize + 1, 
                                                                                     (udrSelect.ptPivot.Y ) * intPixelSize + 1), new Size(intPixelSize, intPixelSize));
                            Bitmap bmpSP_Temp = new Bitmap(bmpZoomed);
                            using (Graphics g = Graphics.FromImage(bmpSP_Temp))
                            {
                                g.DrawRectangle(new Pen(Color.White), recSelectSinglePixel);
                                g.DrawRectangle(new Pen(Color.Black), new Rectangle(recSelectSinglePixel.Left - 1, recSelectSinglePixel.Top - 1, recSelectSinglePixel.Width + 2, recSelectSinglePixel.Height + 2));
                            }
                            pic.Image = bmpSP_Temp;
                        }
                        else
                        {
                            Bitmap bmpSP_Temp = new Bitmap(bmp);
                            using (Graphics g = Graphics.FromImage(bmpSP_Temp))
                            {
                                g.DrawEllipse(new Pen(Color.White), udrSelect.ptPivot.X - 1, udrSelect.ptPivot.Y - 1, 2, 2);
                                g.DrawEllipse(new Pen(Color.Black), udrSelect.ptPivot.X - 2, udrSelect.ptPivot.Y - 2, 4, 4);
                            }
                            pic.Image = bmpSP_Temp;
                        }
                        pic.Refresh();
                        return;
                    }
                    bool bolLeft = (udrSelect.ptDiagonal.X < udrSelect.ptPivot.X);
                    bool bolTop = (udrSelect.ptDiagonal.Y < udrSelect.ptPivot.Y);

                    if (bolLeft && bolTop)
                        udrSelect.setting = enuCorner.TL;
                    else if (bolLeft && !bolTop)
                        udrSelect.setting = enuCorner.BL;
                    else if (!bolLeft && bolTop)
                        udrSelect.setting = enuCorner.TR;
                    else
                        udrSelect.setting = enuCorner.BR;

                    udrSelect.corners.TL = udrSelect.ptDiagonal;
                    udrSelect.corners.BR = udrSelect.ptPivot;

                    switch (udrSelect.setting)
                    {
                        case enuCorner.TL:
                            break;

                        case enuCorner.TR:
                            udrSelect.corners.TL = new Point(udrSelect.ptPivot.X, udrSelect.ptDiagonal.Y);
                            udrSelect.corners.BR = new Point(udrSelect.ptDiagonal.X, udrSelect.ptPivot.Y);
                            break;

                        case enuCorner.BL:
                            udrSelect.corners.TL = new Point(udrSelect.ptDiagonal.X, udrSelect.ptPivot.Y);
                            udrSelect.corners.BR = new Point(udrSelect.ptPivot.X, udrSelect.ptDiagonal.Y);
                            break;

                        case enuCorner.BR:
                            udrSelect.corners.TL = udrSelect.ptPivot;
                            udrSelect.corners.BR = udrSelect.ptDiagonal;
                            break;
                    }

                    udrSelect.corners.BL.X = udrSelect.corners.TL.X;
                    udrSelect.corners.BL.Y = udrSelect.corners.BR.Y;

                    udrSelect.corners.TR.X = udrSelect.corners.BR.X;
                    udrSelect.corners.TR.Y = udrSelect.corners.TL.Y;

                    Size sz = new Size(udrSelect.corners.BR.X - udrSelect.corners.TL.X + 2, udrSelect.corners.BR.Y - udrSelect.corners.TL.Y + 2);
                    //Size szZoom = new Size((sz.Width ) * intPixelSize, (sz.Height) * intPixelSize);

                    Rectangle recSelect = new Rectangle(new Point(udrSelect.corners.TL.X, udrSelect.corners.TL.Y), sz);
                    //Rectangle recSelectZoom = new Rectangle(new Point((udrSelect.corners.TL.X - 1) * intPixelSize + 1, (udrSelect.corners.TL.Y - 1) * intPixelSize + 1), szZoom);

                    udrSelect.bmp.bmpSelected = new Bitmap(recSelect.Width, recSelect.Height);
                    using (Graphics g = Graphics.FromImage(udrSelect.bmp.bmpSelected))
                        g.DrawImage(bmp, new Rectangle(0, 0, sz.Width, sz.Height), recSelect, GraphicsUnit.Pixel);



                    //cLibPicBoxViewer pic2 = new cLibPicBoxViewer(udrSelect.bmp.bmpSelected, "zoom 1");

                    if (intZoomFactor > 1)
                    {
                        udrSelect.bmpZoomed.bmpSelected = resizeBMP(udrSelect.bmp.bmpSelected, intPixelSize);
                        pic.Image = new Bitmap(bmpZoomed);
                    }
                    else
                        pic.Image = new Bitmap(bmp);
                    //drawSelect();
                    drawSelectedRectangle();
                    break;

                case enuSelectMode.hold:
                    if (udrSelect.bmp.bmpSelected == null)
                        return;

                    if (!udrSelect.hold.active)
                    { // area is selected and user can choose to : move, resize or drop
                        int intMinDistance = 2;
                        
                        Point ptTL = udrSelect.corners.TL;
                        Point ptTR = new Point(ptTL.X + udrSelect.bmp.bmpSelected.Width, ptTL.Y);
                        Point ptBL = new Point(ptTL.X, ptTL.Y + udrSelect.bmp.bmpSelected.Height);
                        Point ptBR = new Point(ptTR.X, ptBL.Y);

                        if (ptMouse.X >= ptTL.X
                            && ptMouse.X < ptBR.X
                            && ptMouse.Y >= ptTL.Y
                            && ptMouse.Y < ptBR.Y)
                        {
                            udrSelect.hold.mode = enuSelectHoldMode.move;
                            setCursor(enuCursorTypes.move);
                        }
                        else
                        {
                            bool bolL = false, bolR = false, bolT = false, bolB = false;
                            if (ptMouse.X < ptTL.X && ptMouse.X > ptTL.X - intMinDistance)
                                bolL = true;
                            if (ptMouse.X >= ptBR.X && ptMouse.X < ptBR.X + intMinDistance)
                                bolR = true;
                            if (ptMouse.Y < ptTL.Y && ptMouse.Y > ptTL.Y - intMinDistance)
                                bolT = true;
                            if (ptMouse.Y >= ptBR.Y && ptMouse.Y < ptBR.Y + intMinDistance)
                                bolB = true;
                            if (bolL && bolT)
                            {
                                udrSelect.hold.mode = enuSelectHoldMode.resizeTL;
                                setCursor(enuCursorTypes.resizeTLBR);
                            }
                            else if (bolL && bolB)
                            {
                                udrSelect.hold.mode = enuSelectHoldMode.resizeBL;
                                setCursor(enuCursorTypes.resizeBLTR);
                            }
                            else if (bolR && bolT)
                            {
                                udrSelect.hold.mode = enuSelectHoldMode.resizeTR;
                                setCursor(enuCursorTypes.resizeBLTR);
                            }
                            else if (bolR && bolB)
                            {
                                udrSelect.hold.mode = enuSelectHoldMode.resizeBR;
                                setCursor(enuCursorTypes.resizeTLBR);
                            }
                            else if (bolR
                                     && ptMouse.Y > ptTL.Y
                                     && ptMouse.Y < ptBR.Y)
                            {
                                udrSelect.hold.mode = enuSelectHoldMode.resizeR;
                                setCursor(enuCursorTypes.resizeLeftRight);
                            }
                            else if (bolL
                                     && ptMouse.Y > ptTL.Y
                                     && ptMouse.Y < ptBR.Y)
                            {
                                udrSelect.hold.mode = enuSelectHoldMode.resizeL;
                                setCursor(enuCursorTypes.resizeLeftRight);
                            }
                            else if (bolT
                                     && ptMouse.X > ptTL.X
                                     && ptMouse.X < ptBR.X)
                            {
                                udrSelect.hold.mode = enuSelectHoldMode.resizeT;
                                setCursor(enuCursorTypes.resizeUpDown);
                            }
                            else if (bolB
                                     && ptMouse.X > ptTL.X
                                     && ptMouse.X < ptBR.X)
                            {
                                udrSelect.hold.mode = enuSelectHoldMode.resizeB;
                                setCursor(enuCursorTypes.resizeUpDown);
                            }
                            else
                            {
                                udrSelect.hold.mode = enuSelectHoldMode.ignore;
                                setCursor(enuCursorTypes.arrow);
                                return;
                            }
                        }
                    }
                    else
                    {// hold-active - selected area - user is either in the process of : moving or resizing a selected area
                        int intNewWidth, intNewHeight;
                        Bitmap bmpTemp;
                        if (udrSelect.bmp.bmpSelected == null)
                            return;
                        switch (udrSelect.hold.mode)
                        {
                            case enuSelectHoldMode.move:
                                udrSelect.corners.TR.X = udrSelect.corners.TL.X + udrSelect.bmp.bmpSelected.Width - 1;
                                udrSelect.corners.TR.Y = udrSelect.corners.TL.Y;

                                udrSelect.corners.BL.X = udrSelect.corners.TL.X;
                                udrSelect.corners.BL.Y = udrSelect.corners.TL.Y + udrSelect.bmp.bmpSelected.Height - 1;

                                udrSelect.corners.BR.X = udrSelect.corners.TR.X;
                                udrSelect.corners.BR.Y = udrSelect.corners.BL.Y;
                                if (intZoomFactor > 1)
                                {
                                    bmpTemp = new Bitmap(udrSelect.bmpZoomed.bmpBackground);
                                    udrSelect.hold.ptNewLoc = udrSelect.corners.TL = new Point(ptMouse.X - udrSelect.hold.ptMouseGrabLoc.X , ptMouse.Y - udrSelect.hold.ptMouseGrabLoc.Y );
                                    drawSelect();
                                    break;
                                }
                                else
                                {
                                    bmpTemp = new Bitmap(udrSelect.bmp.bmpBackground);
                                    udrSelect.hold.ptNewLoc = udrSelect.corners.TL = new Point(ptMouse.X - udrSelect.hold.ptMouseGrabLoc.X, ptMouse.Y - udrSelect.hold.ptMouseGrabLoc.Y);
                                    drawSelect();
                                    break;
                                }
                               
                                break;

                            case enuSelectHoldMode.resizeB:
                                // TL 
                                // width 
                                intNewHeight = ptMouse.Y - udrSelect.corners.TL.Y;
                                if (intNewHeight > 1)
                                {
                                    Size szNew = new Size(udrSelect.bmp.bmpRememberSelected.Width, intNewHeight);
                                    udrSelect.bmp.bmpSelected = new Bitmap(udrSelect.bmp.bmpRememberSelected, szNew);
                                    if (intZoomFactor > 1)
                                    {
                                        udrSelect.bmpZoomed.bmpSelected = resizeBMP(udrSelect.bmp.bmpSelected, intPixelSize);
                                        pic.Image = udrSelect.bmpZoomed.bmpBackground;
                                    }
                                    else
                                    {
                                        pic.Image = udrSelect.bmp.bmpBackground;
                                    }

                                    drawSelect();
                                    pic.Refresh();
                                }

                                break;

                            case enuSelectHoldMode.resizeBL:
                                // TR - fixed
                                intNewHeight = ptMouse.Y - udrSelect.corners.TL.Y;
                                intNewWidth = udrSelect.corners.TR.X - ptMouse.X;
                                udrSelect.hold.ptNewLoc.X = udrSelect.corners.TR.X - intNewWidth + 1;

                                if (intNewHeight > 1 && intNewWidth > 1)
                                {
                                    Size szNew = new Size(intNewWidth, intNewHeight);
                                    udrSelect.bmp.bmpSelected = new Bitmap(udrSelect.bmp.bmpRememberSelected, szNew);

                                    if (intZoomFactor > 1)
                                    {
                                        udrSelect.bmpZoomed.bmpSelected = resizeBMP(udrSelect.bmp.bmpSelected, intPixelSize);
                                        pic.Image = udrSelect.bmpZoomed.bmpBackground;
                                    }
                                    else
                                    {
                                        pic.Image = udrSelect.bmp.bmpBackground;
                                    }

                                    drawSelect();
                                    pic.Refresh();

                                }
                                break;

                            case enuSelectHoldMode.resizeBR:
                                // TL 
                                intNewHeight = ptMouse.Y - udrSelect.corners.TL.Y;
                                intNewWidth = ptMouse.X - udrSelect.corners.TL.X;
                                if (intNewHeight > 1 && intNewWidth > 1)
                                {
                                    Size szNew = new Size(intNewWidth, intNewHeight);
                                    udrSelect.bmp.bmpSelected = new Bitmap(udrSelect.bmp.bmpRememberSelected, szNew);

                                    if (intZoomFactor > 1)
                                    {
                                        udrSelect.bmpZoomed.bmpSelected = resizeBMP(udrSelect.bmp.bmpSelected, intPixelSize);
                                        pic.Image = udrSelect.bmpZoomed.bmpBackground;
                                    }
                                    else
                                    {
                                        pic.Image = udrSelect.bmp.bmpBackground;
                                    }

                                    drawSelect();
                                    pic.Refresh();
                                }
                                break;

                            case enuSelectHoldMode.resizeL:
                                // TR 
                                intNewHeight = udrSelect.bmp.bmpSelected.Height;
                                intNewWidth = udrSelect.corners.TR.X - ptMouse.X;
                                udrSelect.hold.ptNewLoc.X = udrSelect.corners.TR.X - intNewWidth + 1;

                                if (intNewHeight > 1 && intNewWidth > 1)
                                {
                                    Size szNew = new Size(intNewWidth, udrSelect.bmp.bmpSelected.Height);
                                    udrSelect.bmp.bmpSelected = new Bitmap(udrSelect.bmp.bmpRememberSelected, szNew);

                                    if (intZoomFactor > 1)
                                    {
                                        udrSelect.bmpZoomed.bmpSelected = resizeBMP(udrSelect.bmp.bmpSelected, intPixelSize);
                                        pic.Image = udrSelect.bmpZoomed.bmpBackground;
                                    }
                                    else
                                    {
                                        pic.Image = udrSelect.bmp.bmpBackground;
                                    }

                                    drawSelect();
                                    pic.Refresh();
                                }
                                break;

                            case enuSelectHoldMode.resizeR: // fixed
                                // TL 
                                // height 
                                intNewWidth = ptMouse.X - udrSelect.corners.TL.X;
                                if (intNewWidth > 1)
                                {
                                    Size szNew = new Size(intNewWidth, udrSelect.bmp.bmpRememberSelected.Height);
                                    udrSelect.bmp.bmpSelected = new Bitmap(udrSelect.bmp.bmpRememberSelected, szNew);

                                    if (intZoomFactor > 1)
                                    {
                                        udrSelect.bmpZoomed.bmpSelected = resizeBMP(udrSelect.bmp.bmpSelected, intPixelSize);
                                        pic.Image = udrSelect.bmpZoomed.bmpBackground;
                                    }
                                    else
                                    {
                                        pic.Image = udrSelect.bmp.bmpBackground;
                                    }

                                    drawSelect();                                    
                                    pic.Refresh();
                                }
                                break;

                            case enuSelectHoldMode.resizeT://fixed
                                // BL 
                                intNewHeight = udrSelect.corners.BL.Y - ptMouse.Y;
                                intNewWidth = udrSelect.bmp.bmpSelected.Width;
                                udrSelect.hold.ptNewLoc.Y = udrSelect.corners.BR.Y - intNewHeight + 1;

                                if (intNewHeight > 1 && intNewWidth > 1)
                                {
                                    Size szNew = new Size(intNewWidth, intNewHeight);
                                    udrSelect.bmp.bmpSelected = new Bitmap(udrSelect.bmp.bmpRememberSelected, szNew);

                                    if (intZoomFactor > 1)
                                    {
                                        udrSelect.bmpZoomed.bmpSelected = resizeBMP(udrSelect.bmp.bmpSelected, intPixelSize);
                                        pic.Image = udrSelect.bmpZoomed.bmpBackground;
                                    }
                                    else
                                    {
                                        pic.Image = udrSelect.bmp.bmpBackground;
                                    }

                                    drawSelect();
                                    pic.Refresh();
                                   
                                }
                                break;

                            case enuSelectHoldMode.resizeTL:
                                // BR 
                                intNewHeight = udrSelect.corners.BR.Y - ptMouse.Y;
                                intNewWidth = udrSelect.corners.TR.X - ptMouse.X;
                                udrSelect.hold.ptNewLoc.X = udrSelect.corners.TR.X - intNewWidth + 1;
                                udrSelect.hold.ptNewLoc.Y = udrSelect.corners.BR.Y - intNewHeight + 1;

                                if (intNewHeight > 1 && intNewWidth > 1)
                                {
                                    Size szNew = new Size(intNewWidth, intNewHeight);
                                    udrSelect.bmp.bmpSelected = new Bitmap(udrSelect.bmp.bmpRememberSelected, szNew);

                                    if (intZoomFactor > 1)
                                    {
                                        udrSelect.bmpZoomed.bmpSelected = resizeBMP(udrSelect.bmp.bmpSelected, intPixelSize);
                                        pic.Image = udrSelect.bmpZoomed.bmpBackground;
                                    }
                                    else
                                    {
                                        pic.Image = udrSelect.bmp.bmpBackground;
                                    }

                                    drawSelect();
                                    pic.Refresh();
                                }
                                break;

                            case enuSelectHoldMode.resizeTR:
                                // BL 
                                intNewHeight = udrSelect.corners.BL.Y - ptMouse.Y;
                                intNewWidth = ptMouse.X - udrSelect.corners.TL.X;
                                udrSelect.hold.ptNewLoc.Y = udrSelect.corners.BL.Y - intNewHeight + 1;

                                if (intNewHeight > 1 && intNewWidth > 1)
                                {
                                    Size szNew = new Size(intNewWidth, intNewHeight);
                                    udrSelect.bmp.bmpSelected = new Bitmap(udrSelect.bmp.bmpRememberSelected, szNew);

                                    if (intZoomFactor > 1)
                                    {
                                        udrSelect.bmpZoomed.bmpSelected = resizeBMP(udrSelect.bmp.bmpSelected, intPixelSize);
                                        pic.Image = udrSelect.bmpZoomed.bmpBackground;
                                    }
                                    else
                                    {
                                        pic.Image = udrSelect.bmp.bmpBackground;
                                    }

                                    drawSelect();
                                    pic.Refresh();

                                } break;
                        }

                    }
                    udrSelect.corners.TL = udrSelect.hold.ptNewLoc;
                    //drawSelectedRectangle();
                    return;
            }
        }

        double distanceBetweenTwoPoints(Point pt1, Point pt2) { return Math.Sqrt(Math.Pow(pt1.X - pt2.X, 2) + Math.Pow(pt1.Y - pt2.Y, 2)); }

        public void initBackups()
        {
            string strWorkingDirectory = System.IO.Directory.GetCurrentDirectory();

            string[] strFile = System.IO.Directory.GetFiles(strWorkingDirectory, BackUpFilename + intMyID_Counter.ToString("d4") + "*" + BackUpExtension);
            for (int intFileCounter = 0; intFileCounter < strFile.Length; intFileCounter++)
                System.IO.File.Delete(strFile[intFileCounter]);
            intBackUpFileID_Max = intBackUpFileID_current = 0;
        }

        public void backupImage()
        {
            intBackUpFileID_current++;
            string strFilename = System.IO.Directory.GetCurrentDirectory() + "\\" + BackUpFilename + intMyID_Counter.ToString("d4") + (intBackUpFileID_current).ToString("d3") + BackUpExtension;
            try
            {
                Bitmap bmpTemp = new Bitmap(bmp);
                bmpTemp.Save(strFilename);
                if (intBackUpFileID_current < intBackUpFileID_Max)
                {
                    string strWorkingDirectory = System.IO.Directory.GetCurrentDirectory();
                    string[] strFile = System.IO.Directory.GetFiles(strWorkingDirectory, BackUpFilename + "???" + BackUpExtension);
                    for (int intFileCounter = intBackUpFileID_current; intFileCounter < strFile.Length; intFileCounter++)
                        System.IO.File.Delete(strFile[intFileCounter]);
                }
            }
            catch (Exception) { initBackups(); }
            intBackUpFileID_Max = intBackUpFileID_current;
            feedback("backed up : " + intBackUpFileID_current.ToString(), new Point ( pic.Location.X,  pic.Location.Y - lblFeedback.Height));
        }

        public void restoreImage_Back()
        {
            if (intBackUpFileID_current >= 1)
            {
                string strFilename = System.IO.Directory.GetCurrentDirectory() + "\\" + BackUpFilename + intMyID_Counter.ToString("d4") + (--intBackUpFileID_current).ToString("d3") + BackUpExtension;
                try
                {
                    PictureBox picTemp = new PictureBox();
                    picTemp.Load(strFilename);
                    bmp = (Bitmap)picTemp.Image;
                }
                catch (Exception) { }
                //cLibPicBoxViewer pic2 = new cLibPicBoxViewer(bmp, "bmp backup");

                feedback("restored back : " + intBackUpFileID_current.ToString(),new Point ( pic.Location.X,  pic.Location.Y - lblFeedback.Height));
                resizeImage();
                initSelectVars();
                pic.Refresh();
            }
        }

        public void restoreImage_Forward()
        {
            string strSearchPath = BackUpFilename + (intBackUpFileID_current).ToString("d3") + "*" + BackUpExtension;

            string[] strFiles = System.IO.Directory.GetFiles(System.IO.Directory.GetCurrentDirectory() + "\\", strSearchPath);
            intBackUpFileID_Max = strFiles.Length;
            if (intBackUpFileID_current < intBackUpFileID_Max)
            {
                intBackUpFileID_current += 1;
                string strFilename = System.IO.Directory.GetCurrentDirectory() + "\\" + BackUpFilename + (intBackUpFileID_current).ToString("d3") + BackUpExtension;
                try
                {
                    PictureBox picTemp = new PictureBox();
                    picTemp.Load(strFilename);
                    bmp = (Bitmap)picTemp.Image;
                }
                catch (Exception) { }
                feedback("restored forward : " + intBackUpFileID_current.ToString(), new Point(pic.Location.X, pic.Location.Y - lblFeedback.Height));
                resizeImage();
                initSelectVars();
            }
        }

        void setZoomFactor(int intNewSetting)
        {
            intZoomFactor = intNewSetting;
            intPixelSize = (intZoomFactor - 1) * 5 + 1;
        }

        public void setImage(Bitmap bmpNew)
        {
            bmp = bmpNew;
            pic.Image = null;
            pic.Refresh();
            setZoomFactor(1);
            resizeImage();
            initBackups();
            backupImage();
            initSelectVars();
        }

        void initSelectVars()
        {
            udrSelect.bmp.bmpBackground = bmp;
            udrSelect.bmp.bmpSelected = null;

            udrSelect.bmpZoomed.bmpBackground = bmpZoomed;
            udrSelect.bmpZoomed.bmpSelected = null;

            udrSelect.mode = enuSelectMode.settingPivot;
            udrSelect.bolEraseBack = true;
            udrSelect.hold.active = false;
        }

        void resizeImage()
        {
            if (mode == enuMode.Select)
                if (udrSelect.mode == enuSelectMode.hold)
                    unselect();

            Bitmap bmpNewSize;
            if (intZoomFactor == 1)
            {
                pic.Image = bmpZoomed = udrSelect.bmp.bmpBackground=bmp;
                udrSelect.bmpZoomed.bmpSelected = udrSelect.bmp.bmpSelected;
                udrSelect.bmpZoomed.bmpBackground = udrSelect.bmp.bmpBackground;
                pic.Refresh();
            }
            else
            {
                int intNewWidth = (bmp.Width > 1) ? (bmp.Width) * intPixelSize : intPixelSize;
                int intNewHeight = (bmp.Height > 1) ? (bmp.Height) * intPixelSize : intPixelSize;

                bmpNewSize = new Bitmap(intNewWidth, intNewHeight);
                if (bmpNewSize.Width + bmpNewSize.Height > 10000)
                    if (MessageBox.Show("large images are slow are you sure you want to resize?", "slow", MessageBoxButtons.YesNo) == DialogResult.No)
                        return;

                int intLeft, intTop;

                modeRemember = mode;
                setMode(enuMode.Magnify);

                using (Graphics g = Graphics.FromImage(bmpNewSize))
                    for (int intX = 0; intX < bmp.Width; intX++)
                    {
                        intLeft = intX  * intPixelSize + 1;
                        for (int intY = 0; intY < bmp.Height; intY++)
                        {
                            intTop = intY * intPixelSize + 1;
                            g.FillRectangle(new SolidBrush(bmp.GetPixel(intX, intY)), intLeft, intTop, intPixelSize, intPixelSize);
                        }
                    }
                initSelectVars();

                pic.Image = udrSelect.bmpZoomed.bmpBackground = bmpZoomed = bmpNewSize;
                setMode(modeRemember);
            }
            placeObjectsOnPanel();
        }

        Bitmap resizeBMP(Bitmap bmpOriginal, int intNewPixelSize)
        {
            if (bmpOriginal == null)
                return null;
            Bitmap bmpNewSize = new Bitmap((bmpOriginal.Width) * intNewPixelSize, (bmpOriginal.Height) * intNewPixelSize);
            int intLeft, intTop;

            using (Graphics g = Graphics.FromImage(bmpNewSize))
                for (int intX = 0; intX < bmpOriginal.Width; intX++)
                {
                    intLeft = intX * intNewPixelSize;
                    for (int intY = 0; intY < bmpOriginal.Height; intY++)
                    {
                        intTop = intY * intNewPixelSize;
                        Color clr = bmpOriginal.GetPixel(intX, intY);
                        g.FillRectangle(new SolidBrush(clr), intLeft, intTop, intNewPixelSize, intNewPixelSize);
                    }
                }

            return bmpNewSize;
        }

        void pnlModeSelect_MouseLeave(object sender, EventArgs e)
        {
            enuMode thisMode = (enuMode)(int)(Math.Floor((double)ptModeSelect.Y / (double)szModeImage.Height));

            if (mode_NextSelection != mode)
                dullModeSelectorPanel(mode_NextSelection);
            hideHelp();
            KillHelpTimer();
            mode_NextSelection = enuMode._numModes;
        }

        void pnlModeSelect_MouseClick(object sender, MouseEventArgs e)
        {
            if (intZoomFactor > 1)
                pic.Image = bmpZoomed;
            else
                pic.Image = bmp;
            pic.Refresh();
            setCursor(enuCursorTypes.arrow);
            pnlFloodFill.Visible = pnlZoom.Visible = false;

            switch (mode_NextSelection)
            {
                case enuMode.redo:
                    restoreImage_Forward();
                    break;

                case enuMode.undo:
                    restoreImage_Back();
                    break;
            }

            dullModeSelectorPanel(mode);
            setMode(mode_NextSelection);
            highlightModeSelectorPanel(mode);
        }

        void lblZoom_MouseClick(object sender, MouseEventArgs e)
        {
            Label thisLabel = (Label)sender;
            if (thisLabel.Text.ToString().CompareTo("Zoom Selected") == 0)
            {
                zoomSelected();
            }
            else

                setZoomFactor(Convert.ToInt32(thisLabel.Text.Substring(1)));

            pnlFloodFill.Visible = pnlZoom.Visible = false;

            resizeImage();
        }

        void zoomSelected()
        {
            Form frmZoomSelect = new Form();
            classGraphicsPicEditor cLibZoomEdit = new classGraphicsPicEditor(frmZoomSelect);

            frmZoomSelect.Controls.Add(cLibZoomEdit);
            cLibZoomEdit.Dock = DockStyle.Fill;
            frmZoomSelect.Owner = callingForm;

            frmZoomSelect.Text = "Zoom Selected Text";
            cLibZoomEdit.setImage(new Bitmap(udrSelect.bmp.bmpSelected));
            cLibZoomEdit.setZoomFactor(3);
            cLibZoomEdit.resizeImage();

            Bitmap bmpTemp = cLibZoomEdit.bmp;

            frmZoomSelect.Visible = true;

            frmZoomSelect.Left = callingForm.Left;
            frmZoomSelect.Top = callingForm.Top;
            frmZoomSelect.Width = callingForm.Width - 100;
            frmZoomSelect.Height = callingForm.Height - 100;
            frmZoomSelect.Visible = false;
            frmZoomSelect.ShowDialog();

            setSelect(new Bitmap(cLibZoomEdit.bmp), udrSelect.hold.ptNewLoc);
            //udrSelect.bmp.bmpSelected =new Bitmap( bmpTemp);

            //udrSelect.bmpZoomed.bmpSelected = resizeBMP(udrSelect.bmp.bmpSelected, intPixelSize);

            frmZoomSelect.Dispose();

            backupImage();
        }

        void lblZoom_MouseLeave(object sender, EventArgs e)
        {
            Label thisLabel = (Label)sender;
            thisLabel.BackColor = Color.White;
            thisLabel.ForeColor = Color.Black;
        }

        void lblZoom_MouseEnter(object sender, EventArgs e)
        {
            Label thisLabel = (Label)sender;
            thisLabel.BackColor = Color.Black;
            thisLabel.ForeColor = Color.White;
        }

        void pnlModeSelect_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.X != ptModeSelect.X || e.Y != ptModeSelect.Y)
                ResetHelpTimer();
            ptModeSelect.X = e.X; ptModeSelect.Y = e.Y;
            enuMode thisMode = (enuMode)(int)(Math.Floor((double)ptModeSelect.Y / (double)szModeImage.Height));

            if (thisMode != mode_NextSelection)
            {
                if (mode_NextSelection != mode)
                {
                    dullModeSelectorPanel(mode_NextSelection);
                    pnlZoom.Visible = false;
                }
                mode_NextSelection = thisMode;
                highlightModeSelectorPanel(mode_NextSelection);

                if (thisMode == enuMode.Flood_Fill)
                {
                    drawFloodFillDisplayPanel();
                    showFloodFill_switchMode_label();
                }
                else if (mode_NextSelection == enuMode.Magnify)
                {
                    /// alt-zoom feature was never completed :
                    /// it initializes by zooming in to a portion of the image and putting that on the screen
                    /// then by keeping track of the Top-Left point(pixel of image, rectangle on display) 
                    /// it shifts the images left/right/up or down when the user's mouse appears close to the edge
                    /// the result was better than zooming onto the entire image and sliding it behind the screen
                    /// but was still too slow to be considered smooth.
                    //setMode(enuMode.alt_Zoom);
                    //pic.Dock = DockStyle.Fill;
                    //initAltZoom();
                    //return;
                    lblFloodFill_changemode.Visible = pnlFloodFill.Visible = pnlZoom.Visible = false;
                    pnlZoom.Dispose();
                    pnlZoom = new Panel();
                    pnlZoom.Width = 0;

                    string[] strZoomLabels = { "x1", "x2", "x3", "x4", "Zoom Selected" };
                    int intNumZooms = (mode == enuMode.Select && udrSelect.mode == enuSelectMode.hold && !udrSelect.hold.active) ? 5 : 4;

                    for (int intZCounter = 0; intZCounter < intNumZooms; intZCounter++)
                    {
                        Label lblZoom = new Label();
                        pnlZoom.Controls.Add(lblZoom);
                        lblZoom.Text = strZoomLabels[intZCounter];
                        lblZoom.AutoSize = true;
                        lblZoom.Left = pnlZoom.Width;
                        pnlZoom.Width = lblZoom.Left + lblZoom.Width;
                        pnlZoom.Height = lblZoom.Height;

                        lblZoom.MouseEnter += new EventHandler(lblZoom_MouseEnter);
                        lblZoom.MouseLeave += new EventHandler(lblZoom_MouseLeave);
                        lblZoom.MouseClick += new MouseEventHandler(lblZoom_MouseClick);
                    }

                    Controls.Add(pnlZoom);
                    pnlZoom.Top = pnlModeSelect.Top + (int)thisMode * GraphicsEditor_CSharp.Properties.Resources.Brush_Icon.Height;
                    pnlZoom.Left = pnlModeSelect.Left + pnlModeSelect.Width;
                    pnlZoom.BringToFront();
                    pnlZoom.Visible = true;
                    HelpType = (enuHelp)(int)thisMode;
                    return;
                }
                else
                    lblFloodFill_changemode.Visible = pnlFloodFill.Visible = false;
            }

            HelpType = (enuHelp)(int)thisMode;
        }

        void showFloodFill_switchMode_label()
        {
            lblFloodFill_changemode.Text = (FloodFill_mode == enuFloodFillMode.sameColor)
                                            ? "switch to 'to Border' mode"
                                            : "switch to 'same Color' mode";
            lblFloodFill_changemode.Top = pnlFloodFill.Top;
            lblFloodFill_changemode.Left = pnlFloodFill.Left + pnlFloodFill.Width;
            lblFloodFill_changemode.Visible = true;
            lblFloodFill_changemode.BringToFront();
        }

        void hideModeObjects()
        {
            pnlZoom.Visible = false;
            pnlFloodFill.Visible = false;
            lblFloodFill_changemode.Visible = false;
        }

        void drawFloodFillDisplayPanel()
        {
            Bitmap bmpFloodfillBackGround = new Bitmap(GraphicsEditor_CSharp.Properties.Resources.FloodFill_pnlBackground_sameColor.Size.Width, GraphicsEditor_CSharp.Properties.Resources.FloodFill_pnlBackground_sameColor.Size.Height);

            using (Graphics g = Graphics.FromImage(bmpFloodfillBackGround))
            {
                g.FillRectangle(new SolidBrush(clrFloodFill_border), new Rectangle(0, 0, bmpFloodfillBackGround.Width, bmpFloodfillBackGround.Height));
                g.FillRectangle(new SolidBrush(clrFloodFill_set), new Rectangle(5, 5, bmpFloodfillBackGround.Height - 10, bmpFloodfillBackGround.Width - 10));

                Bitmap bmpTemp = (FloodFill_mode == enuFloodFillMode.sameColor)
                                ? new Bitmap(GraphicsEditor_CSharp.Properties.Resources.FloodFill_pnlBackground_sameColor)
                                : new Bitmap(GraphicsEditor_CSharp.Properties.Resources.FloodFill_pnlBackground_toBorder);
                bmpTemp.MakeTransparent();

                g.DrawImage(bmpTemp, new Rectangle(0, 0, bmpTemp.Size.Width, bmpTemp.Size.Height));
            }

            pnlFloodFill.BackgroundImage = bmpFloodfillBackGround;
            pnlFloodFill.BringToFront();
            pnlFloodFill.Visible = true;
        }

        void lblFloodFill_changemode_MouseClick(object sender, MouseEventArgs e)
        {
            FloodFill_mode = (enuFloodFillMode)(((int)FloodFill_mode + 1) % 2);
            showFloodFill_switchMode_label();
            drawFloodFillDisplayPanel();
        }

        void pnlFloodFill_MouseClick(object sender, MouseEventArgs e)
        {
            int intBorderWidth = 4;
            if (FloodFill_mode == enuFloodFillMode.toBorder
                && (e.X <= intBorderWidth
                    || e.X >= pnlFloodFill.Width - intBorderWidth
                    || e.Y <= intBorderWidth
                    || e.Y >= pnlFloodFill.Height - intBorderWidth))
                clrFloodFill_border = clr;
            else
                clrFloodFill_set = clr;
            drawFloodFillDisplayPanel();
        }

        void pnlColorSelect_MouseMove(object sender, MouseEventArgs e) { ptClrSelect.X = e.X; ptClrSelect.Y = e.Y; }
        void pnlColorSelect_Click(object sender, EventArgs e) { setClr(((Bitmap)pnlColorSelect.BackgroundImage).GetPixel(ptClrSelect.X, ptClrSelect.Y)); }

        void setClr(Color clrNew)
        {
            clr = clrNew;
            Bitmap bmpColorSelectBackground = (Bitmap)pnlColorSelect.BackgroundImage;

            using (Graphics g = Graphics.FromImage(bmpColorSelectBackground))
                g.FillRectangle(new SolidBrush(clrNew), new Rectangle(7, 6, 11, 11));

            pnlColorSelect.BackgroundImage = bmpColorSelectBackground;
            pnlColorSelect.Refresh();
        }

        void classGraphicsPicEditor_SizeChanged(object sender, EventArgs e) { placeObjectsOnPanel(); }

        void dullModeSelectorPanel(enuMode thisMode)
        {
            Bitmap bmpModeSelectorBackgroundImage = new Bitmap(pnlModeSelect.BackgroundImage);

            if (thisMode != enuMode._numModes)
            {
                using (Graphics g = Graphics.FromImage(bmpModeSelectorBackgroundImage))
                {
                    g.FillRectangle(new SolidBrush(Color.White),
                                    new Rectangle(getSelectModeImageLocation(thisMode),
                                                  new Size(pnlModeSelect.Width,
                                                            GraphicsEditor_CSharp.Properties.Resources.Pencil_Icon.Height)));
                    g.DrawImage(getModeImage(thisMode), getSelectModeImageLocation(thisMode));
                }
                pnlModeSelect.BackgroundImage = bmpModeSelectorBackgroundImage;
            }
        }

        void highlightModeSelectorPanel(enuMode thisMode)
        {
            Bitmap bmpModeSelectorBackgroundImage = new Bitmap(pnlModeSelect.BackgroundImage);

            Bitmap bmpIcon = new Bitmap(getModeImage(thisMode));
            bmpIcon.MakeTransparent();
            Bitmap bmpIconBackground = new Bitmap(pnlModeSelect.Width, bmpIcon.Height);

            using (Graphics g = Graphics.FromImage(bmpIconBackground))
            {
                g.FillRectangle(new SolidBrush(thisMode == mode ? Color.Orange : Color.Yellow), new Rectangle(0, 0, bmpIconBackground.Width, bmpIconBackground.Height));
                g.DrawImage(bmpIcon, new Point(0, 0));
            }

            using (Graphics g = Graphics.FromImage(bmpModeSelectorBackgroundImage))
                g.DrawImage(bmpIconBackground, getSelectModeImageLocation(thisMode));

            pnlModeSelect.BackgroundImage = bmpModeSelectorBackgroundImage;
        }

        void drawModeSelectorPanel()
        {
            pnlModeSelect.Size = new Size((int)(szModeImage.Width * 1.5) + 2, szModeImage.Height * (int)enuMode._numModes + 2);
            Bitmap bmpModeSelectorBackgroundImage = new Bitmap(pnlModeSelect.Width, pnlModeSelect.Height);

            using (Graphics g = Graphics.FromImage(bmpModeSelectorBackgroundImage))
            {
                for (enuMode modeCounter = 0; modeCounter < enuMode._numModes; modeCounter++)
                    g.DrawImage(getModeImage(modeCounter), getSelectModeImageLocation(modeCounter));
            }

            pnlModeSelect.BackgroundImage = bmpModeSelectorBackgroundImage;
            highlightModeSelectorPanel(mode);
        }

        enum sbNeeded { no, yes, onlyIfTheOtherIsVisible }
        void placeObjectsOnPanel()
        {
            if (Height < 100 || Width < 100)
                return;

            // color controls panel
            pnlColorSelect.Height = GraphicsEditor_CSharp.Properties.Resources.colorTemplate.Height + 2;
            pnlColorSelect.Width = GraphicsEditor_CSharp.Properties.Resources.colorTemplate.Width + 2;
            pnlColorSelect.BackgroundImage = new Bitmap(GraphicsEditor_CSharp.Properties.Resources.colorTemplate); ;
            pnlColorSelect.Left = 1; pnlColorSelect.Top = conVerticalOffset;
            pnlColorSelect.Visible = true;

            // mode control panel
            drawModeSelectorPanel();
            pnlModeSelect.Left = pnlColorSelect.Left;
            pnlModeSelect.Top = pnlColorSelect.Top + pnlColorSelect.Height + 1;
            if (pnlModeSelect.Top + pnlModeSelect.Height > Height)
                pnlModeSelect.Location = new Point(pnlColorSelect.Left + pnlColorSelect.Width + 1, pnlColorSelect.Top);
            pnlModeSelect.Visible = true;

            pnlFloodFill.Top = pnlModeSelect.Top + (int)enuMode.Flood_Fill * szModeImage.Height;
            pnlFloodFill.Left = pnlModeSelect.Left + pnlModeSelect.Width;

            // place scroll bars
            sbNeeded vsbNeeded = sbNeeded.no;
            sbNeeded hsbNeeded = sbNeeded.no;
            int intWidthAvailable = Width - pnlModeSelect.Left - pnlModeSelect.Width - 12;
            int intHeightAvailable = Height - 12 - conVerticalOffset;

            pic.Size = new Size(pic.Image.Size.Width + 15, pic.Image.Size.Height + 15);

            if (pic.Width > intWidthAvailable)
                hsbNeeded = sbNeeded.yes;
            else if (pic.Width > intWidthAvailable - vsb.Width)
                hsbNeeded = sbNeeded.onlyIfTheOtherIsVisible;

            if (pic.Height > intHeightAvailable)
                vsbNeeded = sbNeeded.yes;
            else if (pic.Height > intHeightAvailable - hsb.Height)
                vsbNeeded = sbNeeded.onlyIfTheOtherIsVisible;

            if (vsbNeeded == sbNeeded.onlyIfTheOtherIsVisible && hsbNeeded == sbNeeded.yes)
                vsbNeeded = sbNeeded.yes;

            if (hsbNeeded == sbNeeded.onlyIfTheOtherIsVisible && vsbNeeded == sbNeeded.yes)
                hsbNeeded = sbNeeded.yes;

            if (hsbNeeded == sbNeeded.yes)
            {
                double dblFractionValue = 0;
                if (hsb.Visible)
                    dblFractionValue = (double)hsb.Value / (double)(hsb.Maximum - hsb.LargeChange);
                if (dblFractionValue > 1)
                    dblFractionValue = 1;

                hsb.Left = pnlModeSelect.Left + pnlModeSelect.Width + 10;
                hsb.Top = Height - hsb.Height - 5;
                hsb.Width = intWidthAvailable - (vsbNeeded == sbNeeded.yes ? vsb.Width : 0);

                double dblPercentImageExtra = ((double)pic.Width - (double)hsb.Width) / (double)pic.Width;

                hsb.LargeChange = hsb.Maximum - (int)(dblPercentImageExtra * (double)hsb.Maximum);
                hsb.SmallChange = (int)(Math.Ceiling((double)hsb.LargeChange / 8.0));

                hsb.Value = (int)(dblFractionValue * (double)(hsb.Maximum - hsb.LargeChange));

                hsb.Visible = true;
                hsb.BringToFront();
            }
            else
            {
                hsb.Visible = false;
                pic.Left = pnlModeSelect.Left + pnlModeSelect.Width + 5;
            }

            if (vsbNeeded == sbNeeded.yes)
            {
                double dblFractionValue = 0;
                if (vsb.Visible)
                    dblFractionValue = (double)vsb.Value / (double)(vsb.Maximum - vsb.LargeChange);
                if (dblFractionValue > 1)
                    dblFractionValue = 1;

                vsb.Top = 5 + conVerticalOffset;
                vsb.Left = Width - vsb.Width - 5;
                vsb.Height = intHeightAvailable - (hsbNeeded == sbNeeded.yes ? hsb.Height : 0);

                vsb.LargeChange = (int)(((double)(vsb.Height) / (double)pic.Height) * vsb.Maximum);
                vsb.SmallChange = (int)(Math.Ceiling((double)vsb.LargeChange / 8.0));

                vsb.Value = (int)(dblFractionValue * (double)(vsb.Maximum - vsb.LargeChange));

                vsb.Visible = true;
                vsb.BringToFront();
            }
            else
            {
                vsb.Visible = false;
                pic.Top = conVerticalOffset;
            }

            progressBar.Left = progressBar.Width = Width / 3;

            pic.Visible = true;
        }

        void hsb_ValueChanged(object sender, EventArgs e)
        {
            int intMinLeft = hsb.Left + hsb.Width - pic.Width;
            int intMaxLeft = hsb.Left;
            double dblDifference = intMaxLeft - intMinLeft;

            double dblPercentageOffScreenLeft = (double)hsb.Value / (double)(hsb.Maximum - hsb.LargeChange);
            pic.Left = intMaxLeft - (int)(dblPercentageOffScreenLeft * dblDifference);
        }

        void vsb_ValueChanged(object sender, EventArgs e)
        {
            int intMinTop = vsb.Top + vsb.Height - pic.Height;
            int intMaxTop = vsb.Top;
            double dblDifference = intMaxTop - intMinTop;

            double dblPercentageOffScreenTop = (double)vsb.Value / (double)(vsb.Maximum - vsb.LargeChange);
            pic.Top = intMaxTop - (int)(dblPercentageOffScreenTop * dblDifference);
        }

        Size ModeSelectorImageSize()
        {
            int intNumRows = (int)Math.Ceiling(((double)enuMode._numModes) / 2.0);
            return new Size(2 * szModeImage.Width, intNumRows * szModeImage.Height);
        }
        Size ModeImageSize() { return GraphicsEditor_CSharp.Properties.Resources.Select_Icon.Size; }
        Point getSelectModeImageLocation(enuMode thisMode) { return new Point(0, szModeImage.Height * (int)thisMode); }


        /* SWITCHMODE TEMPLATE
          switch (thisMode)
            {
                case enuMode.Brush:
                    break;
                case enuMode.Eraser:
                    break;

                case enuMode.FloodFill:
                    break;

                case enuMode.FreeFormSelect:
                    break;
                case enuMode.Line:
                    break;

                case enuMode.Magnify:
                    break;

                case enuMode.Pick_Color:
                    break;

                case enuMode.Select:
                    break;

                default:
                    break;
            }
         */

        /// <summary>
        /// returns image associated with this mode
        /// </summary>
        /// <returns>bitmap</returns>
        Bitmap getModeImage() { return getModeImage(mode); }

        /// <summary>
        /// returns image associated with this mode
        /// </summary>
        /// <param name="thisMode">the mode for which image is required</param>
        /// <returns>bitmap</returns>
        Bitmap getModeImage(enuMode thisMode)
        {
            switch (thisMode)
            {
                case enuMode.Brush:
                    return GraphicsEditor_CSharp.Properties.Resources.Brush_Icon;

                case enuMode.Eraser:
                    return GraphicsEditor_CSharp.Properties.Resources.Eraser_Icon;

                case enuMode.Flood_Fill:
                    return GraphicsEditor_CSharp.Properties.Resources.FloodFill_Icon;

                case enuMode.Free_Form_Select:
                    return GraphicsEditor_CSharp.Properties.Resources.Free_Form_Select_Icon;

                case enuMode.Line:
                    return GraphicsEditor_CSharp.Properties.Resources.Line_Icon;

                case enuMode.Magnify:
                    return GraphicsEditor_CSharp.Properties.Resources.Magnify_Icon;

                case enuMode.Pick_Color:
                    return GraphicsEditor_CSharp.Properties.Resources.Pick_color_Icon;

                case enuMode.Select:
                    return GraphicsEditor_CSharp.Properties.Resources.Select_Icon;

                case enuMode.Straighten:
                    return GraphicsEditor_CSharp.Properties.Resources.Straighten_Icon;

                case enuMode.undo:
                    return GraphicsEditor_CSharp.Properties.Resources.Edit_Undo_Icon;

                case enuMode.redo:
                    return GraphicsEditor_CSharp.Properties.Resources.Edit_Redo_Icon;

                default:
                    return GraphicsEditor_CSharp.Properties.Resources.Pencil_Icon;
            }
        }

        void enQ(ref udtQ picQ, ref classPixelPos Qele)
        {
            if (picQ.tail == null)
                picQ.tail = Qele;
            else
                picQ.tail.next = Qele;
            Qele.next = null;
            picQ.tail = Qele;

            if (picQ.head == null)
                picQ.head = picQ.tail;
        }

        classPixelPos deQ(ref udtQ picQ)
        {
            classPixelPos Qret = picQ.head;
            picQ.head = picQ.head.next;
            Qret.next = null;
            return Qret;
        }

        Point move(Point pt, enuDirections dir)
        {
            Point ptRetVal = new Point();
            ptRetVal.X = pt.X;
            ptRetVal.Y = pt.Y;

            switch (dir)
            {
                case enuDirections.north:
                    ptRetVal.Y--;
                    break;

                case enuDirections.east:
                    ptRetVal.X++;
                    break;

                case enuDirections.south:
                    ptRetVal.Y++;
                    break;

                case enuDirections.west:
                    ptRetVal.X--;
                    break;
            }

            return ptRetVal;
        }

        public void addPixilateColorFound(ref udtPixilateColorFind[] udrPixilateColorFind, Color clrFound)
        {
            if (udrPixilateColorFind == null)
            {
                udrPixilateColorFind = new udtPixilateColorFind[1];
                udrPixilateColorFind[0] = new udtPixilateColorFind();
                udrPixilateColorFind[0].clr = clrFound;
                udrPixilateColorFind[0].numFound = 1;
            }
            else
            {
                for (int intClrCounter = 0; intClrCounter < udrPixilateColorFind.Length; intClrCounter++)
                {
                    if (udrPixilateColorFind[intClrCounter].clr == clrFound)
                    {
                        udrPixilateColorFind[intClrCounter].numFound++;
                        return;
                    }
                }
                Array.Resize<udtPixilateColorFind>(ref udrPixilateColorFind, udrPixilateColorFind.Length + 1);
                udrPixilateColorFind[udrPixilateColorFind.Length - 1] = new udtPixilateColorFind();
                udrPixilateColorFind[udrPixilateColorFind.Length - 1].clr = clrFound;
                udrPixilateColorFind[udrPixilateColorFind.Length - 1].numFound = 1;
            }
        }

        public void reorderPixilateColorFound(ref udtPixilateColorFind[] udrPixilateColorFind)
        {
            int intBestFind;
            for (int intOuterLoop = 0; intOuterLoop < udrPixilateColorFind.Length - 1; intOuterLoop++)
            {
                intBestFind = intOuterLoop;
                for (int intInnerLoop = intOuterLoop + 1; intInnerLoop < udrPixilateColorFind.Length; intInnerLoop++)
                {
                    if (udrPixilateColorFind[intInnerLoop].numFound > udrPixilateColorFind[intBestFind].numFound)
                        intBestFind = intInnerLoop;
                }
                if (intBestFind != intOuterLoop)
                {
                    udtPixilateColorFind udrTemp = udrPixilateColorFind[intOuterLoop];
                    udrPixilateColorFind[intOuterLoop] = udrPixilateColorFind[intBestFind];
                    udrPixilateColorFind[intBestFind] = udrTemp;
                }
            }
        }

        private void mnuPixilate_Click(object sender, EventArgs e)
        {
            intPixelSize = (intZoomFactor - 1) * 5 + 1;
            Bitmap bmpSource = bmp;
            Bitmap bmpDestination = new Bitmap(bmpSource.Width, bmpSource.Height);
            int intPix_X = bmpSource.Width / intPixelSize;
            int intPix_Y = bmpSource.Height / intPixelSize;
            int intMin = (int)(((double)(intPixelSize * intPixelSize)) / 2.0);

            using (Graphics g = Graphics.FromImage(bmpDestination))
            {
                for (int intX = 0; intX < intPix_X; intX++)
                    for (int intY = 0; intY < intPix_Y; intY++)
                    {
                        Point pt = new Point(intX * intPixelSize, intY * intPixelSize);
                        udtPixilateColorFind[] udrPixilateColorFind = null;

                        for (int x = 0; x < intPixelSize; x++)
                            for (int y = 0; y < intPixelSize; y++)
                            {
                                Point ptTest = new Point(pt.X + x, pt.Y + y);
                                Color clrFound = bmpSource.GetPixel(ptTest.X, ptTest.Y);
                                addPixilateColorFound(ref udrPixilateColorFind, clrFound);
                            }
                        reorderPixilateColorFound(ref udrPixilateColorFind);
                        g.FillRectangle(new SolidBrush(udrPixilateColorFind[0].clr), pt.X, pt.Y, intPixelSize, intPixelSize);
                    }
            }

            pic.Image = bmpDestination;
            pic.Refresh();
        }


        Bitmap StraightenImage(Bitmap bmpInput, double dblAngleRotation)
        {
            int intMinSquare = bmpInput.Width > bmpInput.Height ?
                                3 * bmpInput.Width
                                : 3 * bmpInput.Height;

            Bitmap bmpCopy = new Bitmap(intMinSquare, intMinSquare);
            Point ptTL;
            Point ptBR;
            Point ptCopyCenter = new Point(bmpCopy.Width / 2, bmpCopy.Height / 2);
            Point ptOriginalCenter = new Point(bmpInput.Width / 2, bmpInput.Height / 2);
            using (Graphics g = Graphics.FromImage(bmpCopy))
            {
                g.FillRectangle(new SolidBrush(Color.White), new Rectangle(new Point(0, 0), bmpCopy.Size));                 
                ptTL = new Point(ptCopyCenter.X, ptCopyCenter.Y);
                ptBR = new Point(ptCopyCenter.X, ptCopyCenter.Y);

                for (int intX = 0; intX < bmpInput.Width; intX++)
                {
                    for (int intY = 0; intY < bmpInput.Height; intY++)
                    {
                        int intDX = ptOriginalCenter.X - intX;
                        int intDY = ptOriginalCenter.Y - intY;
                        classMath.classRadialCoor udrOriginalRad = new classMath.classRadialCoor(cLibMath.arcTan(intDX, intDY), Math.Sqrt(intDX * intDX + intDY * intDY));
                        classMath.classRadialCoor udrCopyRad = new classMath.classRadialCoor(udrOriginalRad.angle - dblAngleRotation, udrOriginalRad.radius);
                        Point ptCopy = new Point((int)(ptCopyCenter.X + udrCopyRad.radius * Math.Cos(udrCopyRad.angle)),
                                                 (int)(ptCopyCenter.Y + udrCopyRad.radius * Math.Sin(udrCopyRad.angle)));

                        try { bmpCopy.SetPixel(ptCopy.X, ptCopy.Y, bmpInput.GetPixel(intX, intY)); }
                        catch (Exception) { }

                        // keep track of limits of output image
                        if (ptCopy.X < ptTL.X)
                            ptTL.X = ptCopy.X;
                        if (ptCopy.Y < ptTL.Y)
                            ptTL.Y = ptCopy.Y;
                        if (ptCopy.X > ptBR.X)
                            ptBR.X = ptCopy.X;
                        if (ptCopy.Y > ptBR.Y)
                            ptBR.Y = ptCopy.Y;
                    }
                }
            }

            // write image to output array
            Size szOfImage = new Size(ptBR.X - ptTL.X, ptBR.Y - ptTL.Y);
            Rectangle recSrc = new Rectangle(ptTL, szOfImage);
            Rectangle recDest = new Rectangle(new Point(0, 0), szOfImage);

            Color clrTransparent = bmpCopy.GetPixel(0, 0);
            
            // fill the blanks missed when rotating image
            // scanning x from 0 to width created a problem in the fill-blank
             for (int intX = ptTL.X; intX < ptBR.X; intX++)
                for (int intY = ptTL.Y; intY < ptBR.Y; intY ++)
                {
                    Color clrTest = bmpCopy.GetPixel(intX, intY);
                    if (clrTest == clrTransparent)
                    {
                        classGraphicsPicEditor.udtPixilateColorFind[] udrColorFind = new classGraphicsPicEditor.udtPixilateColorFind[0];
                        if (intX > 0 && intX < bmpCopy.Width - 2 && intY > 0 && intY < bmpCopy.Height - 2)
                        {
                            Point ptUp = new Point(intX, intY - 1);
                            Point ptDown = new Point(intX, intY + 1);
                            Point ptLeft = new Point(intX - 1, intY);
                            Point ptRight = new Point(intX + 1, intY);
                            Point ptUL = new Point(intX - 1, intY - 1);
                            Point ptUR = new Point(intX + 1, intY - 1);
                            Point ptDR = new Point(intX + 1, intY + 1);
                            Point ptDL = new Point(intX - 1, intY + 1);

                            addPixilateColorFound(ref udrColorFind, bmpCopy.GetPixel(ptUp.X, ptUp.Y));
                            addPixilateColorFound(ref udrColorFind, bmpCopy.GetPixel(ptDown.X, ptDown.Y));
                            addPixilateColorFound(ref udrColorFind, bmpCopy.GetPixel(ptLeft.X, ptLeft.Y));
                            addPixilateColorFound(ref udrColorFind, bmpCopy.GetPixel(ptRight.X, ptRight.Y));
                            addPixilateColorFound(ref udrColorFind, bmpCopy.GetPixel(ptDL.X, ptDL.Y));
                            addPixilateColorFound(ref udrColorFind, bmpCopy.GetPixel(ptDR.X, ptDR.Y));
                            addPixilateColorFound(ref udrColorFind, bmpCopy.GetPixel(ptUL.X, ptUL.Y));
                            addPixilateColorFound(ref udrColorFind, bmpCopy.GetPixel(ptUR.X, ptUR.Y));
                            reorderPixilateColorFound(ref udrColorFind);
                            if (udrColorFind[0].numFound >= 5)
                                try { bmpCopy.SetPixel(intX, intY, udrColorFind[0].clr); }
                                catch (Exception) { }
                            else
                            {
                                int intDX = ptCopyCenter.X - intX;
                                int intDY = ptCopyCenter.Y - intY;
                                classMath.classRadialCoor udrOriginalRad = new classMath.classRadialCoor(cLibMath.arcTan(intDX, intDY), Math.Sqrt(intDX * intDX + intDY * intDY));
                                classMath.classRadialCoor udrCopyRad = new classMath.classRadialCoor(udrOriginalRad.angle + dblAngleRotation, udrOriginalRad.radius);
                                Point ptCopy = new Point((int)(ptOriginalCenter.X + udrCopyRad.radius * Math.Cos(udrCopyRad.angle)),
                                                         (int)(ptOriginalCenter.Y + udrCopyRad.radius * Math.Sin(udrCopyRad.angle)));

                                if (ptCopy.X >= 0 && ptCopy.X < bmpCopy.Width && ptCopy.Y >= 0 && ptCopy.Y < bmpCopy.Height)
                                    try { bmpCopy.SetPixel(intX, intY, bmpInput.GetPixel(ptCopy.X, ptCopy.Y)); }
                                    catch (Exception) { }
                            }

                        }
                    }
                }

            Bitmap bmpTemp = new Bitmap(recDest.Width, recDest.Height);

            using (Graphics g = Graphics.FromImage(bmpTemp))
            {
                g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, bmpTemp.Width, bmpTemp.Height));
                g.DrawImage(bmpCopy, recDest, recSrc, GraphicsUnit.Pixel);
            }

           return bmpTemp;
        }

        public void setSelect(Bitmap bmpNewSelect, Point pt)
        {   
            udrSelect.bmp.bmpSelected =new Bitmap( bmpNewSelect);
            if (intZoomFactor > 1)
                udrSelect.bmpZoomed.bmpSelected = resizeBMP(udrSelect.bmp.bmpSelected, intPixelSize);
      
            udrSelect.hold.ptNewLoc = pt;
            udrSelect.corners.TL = pt;

            udrSelect.corners.TR.X = udrSelect.corners.TL.X + udrSelect.bmp.bmpSelected.Width;
            udrSelect.corners.TR.Y = udrSelect.corners.TL.Y;

            udrSelect.corners.BL.X = udrSelect.corners.TL.X;
            udrSelect.corners.BL.Y = udrSelect.corners.TL.Y + udrSelect.bmp.bmpSelected.Height;

            udrSelect.corners.BR.X = udrSelect.corners.TR.X;
            udrSelect.corners.BR.Y = udrSelect.corners.BL.Y;

            if (udrSelect.bmp.bmpSelected.Width > udrSelect.bmp.bmpBackground.Width)
            {
                Bitmap bmpNewBackWidth = new Bitmap(udrSelect.bmp.bmpSelected.Width, udrSelect.bmp.bmpBackground.Height);
                using (Graphics g = Graphics.FromImage(bmpNewBackWidth))
                {
                    g.FillRectangle(new SolidBrush(Color.White), 0, 0, bmpNewBackWidth.Width, bmpNewBackWidth.Height);
                    g.DrawImage(udrSelect.bmp.bmpBackground, new Point(0, 0));
                }
                bmp = udrSelect.bmp.bmpBackground = bmpNewBackWidth;
            }

            if (udrSelect.bmp.bmpSelected.Height > udrSelect.bmp.bmpBackground.Height)
            {
                Bitmap bmpNewBackHeight = new Bitmap(udrSelect.bmp.bmpBackground.Width, udrSelect.bmp.bmpSelected.Height);
                using (Graphics g = Graphics.FromImage(bmpNewBackHeight))
                {
                    g.FillRectangle(new SolidBrush(Color.White), 0, 0, bmpNewBackHeight.Width, bmpNewBackHeight.Height);
                    g.DrawImage(udrSelect.bmp.bmpBackground, new Point(0, 0));
                }
                bmp = udrSelect.bmp.bmpBackground = bmpNewBackHeight;
            }

            if (intZoomFactor > 1)
                udrSelect.bmpZoomed.bmpSelected = resizeBMP(udrSelect.bmp.bmpSelected, intPixelSize);

            placeObjectsOnPanel();
            pic.Refresh();

            dullModeSelectorPanel(mode);
            mode = enuMode.Select;
            highlightModeSelectorPanel(mode);

            picMouseMove_Select();
            udrSelect.mode = enuSelectMode.hold;
            udrSelect.hold.mode = enuSelectHoldMode.ignore;
            udrSelect.hold.active = false;
            udrSelect.bolEraseBack = false;
            
        }

        private void mnuEdit_Undo_Click(object sender, EventArgs e) { restoreImage_Back(); }
        private void mnuEdit_Redo_Click(object sender, EventArgs e) { restoreImage_Forward(); }
        private void mnuFile_Exit_Click(object sender, EventArgs e) { Dispose(); }
        private void mnuEdit_Cut_Click(object sender, EventArgs e)
        {
            if (mode == enuMode.Select)
                if (udrSelect.mode == enuSelectMode.hold)
                {
                    if (!udrSelect.hold.active)
                    {
                        if (!udrSelect.hold.active)
                        {
                            System.Windows.Forms.Clipboard.SetImage(udrSelect.bmp.bmpSelected);
                            if (udrSelect.bolEraseBack)
                            {
                                eraseSelectedFromBackground();
                            }

                            if (intZoomFactor > 1)
                                pic.Image = bmpZoomed;
                            else
                                pic.Image = bmp;

                            pic.Refresh();
                            udrSelect.mode = enuSelectMode.settingPivot;
                            backupImage();
                        }
                    }
                }
        }

        private void eraseSelectedFromBackground()
        {
            if (udrSelect.bmp.bmpSelected != null)
            {
                using (Graphics g = Graphics.FromImage(bmp))
                    g.FillRectangle(new SolidBrush(Color.White), new Rectangle(udrSelect.hold.ptNewLoc, udrSelect.bmp.bmpSelected.Size));
                if (intZoomFactor > 1)
                {
                    using (Graphics g = Graphics.FromImage(bmpZoomed))
                        g.FillRectangle(new SolidBrush(Color.White),
                                        new Rectangle(new Point((udrSelect.hold.ptNewLoc.X ) * intPixelSize + 1,
                                                                ((udrSelect.hold.ptNewLoc.Y ) * intPixelSize + 1)),
                                                      udrSelect.bmpZoomed.bmpSelected.Size));
                }
            }
        }

        private void mnuEdit_SelectAll_Click(object sender, EventArgs e)
        {
            setSelect(new Bitmap(bmp), new Point(0, 0));
            
            
            bmp = new Bitmap(bmp);
            udrSelect.bmp.bmpBackground = new Bitmap(bmp);
            if (intZoomFactor > 1)
            {
                udrSelect.bmpZoomed.bmpBackground = new Bitmap(bmpZoomed.Width, bmpZoomed.Height);
                bmpZoomed = new Bitmap(bmpZoomed.Width, bmpZoomed.Height);
                pic.Image = bmpZoomed;
            }
            else
                pic.Image = bmp;
            udrSelect.bolEraseBack = true;
            drawSelect();
        }

        private void mnuEdit_Copy_Click(object sender, EventArgs e)
        {
            if (mode == enuMode.Select)
                if (udrSelect.mode == enuSelectMode.hold)
                    if (!udrSelect.hold.active)
                        System.Windows.Forms.Clipboard.SetImage(udrSelect.bmp.bmpSelected);
        }

        private void mnuEdit_Paste_Click(object sender, EventArgs e)
        {
            if (mode == enuMode.Select)
            {
                if (System.Windows.Forms.Clipboard.ContainsImage())
                {
                    Bitmap bmpTemp = (Bitmap)System.Windows.Forms.Clipboard.GetImage();
                    if (bmpTemp != null)
                    {
                        if (udrSelect.mode == enuSelectMode.hold)
                            unselect();

                        if (intZoomFactor > 1 && udrSelect.bmpZoomed.bmpBackground == null)
                            udrSelect.bmpZoomed.bmpBackground = resizeBMP(udrSelect.bmp.bmpBackground, intPixelSize);

                        if (udrSelect.bmp.bmpBackground == null)
                            udrSelect.bmp.bmpBackground = bmp;

                        setSelect(bmpTemp, new Point(1, 1));
                        if (bmpTemp.Width > bmp.Width)
                        {
                            Bitmap bmpNewSizeWidth = new Bitmap(bmpTemp.Width, bmp.Height);
                            using (Graphics g = Graphics.FromImage(bmpNewSizeWidth))
                            {
                                g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, bmpNewSizeWidth.Width, bmpNewSizeWidth.Height));
                                g.DrawImage(bmp, new Point(0, 0));
                            }
                            bmp = new Bitmap(bmpNewSizeWidth);
                        }

                        if (bmpTemp.Height > bmp.Height)
                        {
                            Bitmap bmpNewSizeHeight = new Bitmap(bmp.Width, bmpTemp.Height);
                            using (Graphics g = Graphics.FromImage(bmpNewSizeHeight))
                            {
                                g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, bmpNewSizeHeight.Width, bmpNewSizeHeight.Height));
                                g.DrawImage(bmp, new Point(0, 0));
                            }
                            bmp = new Bitmap(bmpNewSizeHeight);
                        }

                        if (intZoomFactor > 1)
                        {
                            bmpZoomed = resizeBMP(bmp, intPixelSize);
                            pic.Image = bmpZoomed;
                        }
                        else
                            pic.Image = bmp;

                        pic.Width = pic.Image.Width + 10;
                        pic.Height = pic.Image.Height + 10;
                        placeObjectsOnPanel();

                        picMouseMove_Select();
                        backupImage();
                    }
                    else
                        ;
                    drawSelect();
                    drawSelectedRectangle();
                }
            }
        }

        private void mnuImage_DrawOpaque_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem thisToolStrip = (ToolStripMenuItem)sender;
            udrSelect.hold.drawOpaque = (thisToolStrip.CheckState == CheckState.Checked);

            if (mode == enuMode.Select
                    && udrSelect.mode == enuSelectMode.hold
                    && udrSelect.hold.mode != enuSelectHoldMode.ignore)
            {

                if (thisToolStrip.CheckState == CheckState.Checked)
                {
                    if (intZoomFactor > 1)
                        udrSelect.bmpZoomed.bmpSelected.MakeTransparent();
                    else
                        udrSelect.bmp.bmpSelected.MakeTransparent();

                }
                else
                {
                    if (intZoomFactor > 1)
                    {
                        Bitmap bmpSelected = new Bitmap(udrSelect.bmpZoomed.bmpSelected.Width, udrSelect.bmpZoomed.bmpSelected.Height);
                        using (Graphics g = Graphics.FromImage(bmpSelected))
                        {
                            g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, bmpSelected.Width, bmpSelected.Height));
                            g.DrawImage(udrSelect.bmpZoomed.bmpSelected, new Point(1, 1));
                        }
                        udrSelect.bmpZoomed.bmpSelected = new Bitmap(bmpSelected);
                    }
                    else
                    {
                        Bitmap bmpSelected = new Bitmap(udrSelect.bmp.bmpSelected.Width, udrSelect.bmp.bmpSelected.Height);
                        using (Graphics g = Graphics.FromImage(bmpSelected))
                        {
                            g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, bmpSelected.Width, bmpSelected.Height));
                            g.DrawImage(udrSelect.bmp.bmpSelected, new Point(1, 1));
                        }
                        udrSelect.bmp.bmpSelected = new Bitmap(bmpSelected);
                    }
                }
                drawSelect();
            }
        }

        private void mnuImage_FlipRotate_Click(object sender, EventArgs e)
        {
            if (udrSelect.bmp.bmpSelected == null)
                return;
            if (frmFlipRotate != null)
            {
                frmFlipRotate.Visible = false;
                frmFlipRotate.Dispose();
            }

            frmFlipRotate = new formFlipRotate();
            frmFlipRotate.Owner = callingForm;


            frmFlipRotate.ShowDialog();

            Bitmap bmpTemp = new Bitmap(udrSelect.bmp.bmpSelected);

            switch (frmFlipRotate.reply)
            {
                case formFlipRotate.enuResponse.flipH:
                    bmpTemp.RotateFlip(RotateFlipType.RotateNoneFlipX);
                    break;

                case formFlipRotate.enuResponse.flipV:
                    bmpTemp.RotateFlip(RotateFlipType.RotateNoneFlipY);
                    break;

                case formFlipRotate.enuResponse.R90:
                    bmpTemp.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    break;

                case formFlipRotate.enuResponse.R180:
                    bmpTemp.RotateFlip(RotateFlipType.Rotate180FlipNone);
                    break;

                case formFlipRotate.enuResponse.R270:
                    bmpTemp.RotateFlip(RotateFlipType.Rotate270FlipNone);
                    break;

                case formFlipRotate.enuResponse.cancel:
                    return;
            }

            setSelect(bmpTemp, udrSelect.hold.ptNewLoc);
            drawSelect();
        }

        private void mnuImage_click(object sender, EventArgs e)
        {
            mnuImage_FlipRotate.Enabled = (mode == enuMode.Select
                                           && udrSelect.mode == enuSelectMode.hold
                                           && (udrSelect.hold.mode == enuSelectHoldMode.ignore
                                                || udrSelect.hold.mode == enuSelectHoldMode.move ));
        }


        private void mnuImage_Attributes_Click(object sender, EventArgs e)
        {
            string strOk = "Ok";
            string strCancel = "Cancel";
            classMessageBox msgBox = new classMessageBox("", "Attributes", strOk, strCancel);

            TextBox txtWidth = new TextBox(); txtWidth.Text = (pic.Image.Width / intPixelSize).ToString(); txtWidth.Visible = true;
            txtWidth.TextAlign = HorizontalAlignment.Right;
            Label lblWidth = new Label(); lblWidth.Text = "Width"; lblWidth.Visible = true;

            TextBox txtHeight = new TextBox(); txtHeight.Text = (pic.Image.Height / intPixelSize).ToString(); txtHeight.Visible = true;
            txtHeight.TextAlign = HorizontalAlignment.Right;
            Label lblHeight = new Label(); lblHeight.Text = "Height"; lblHeight.Visible = true;

            msgBox.Controls.Add(txtWidth);
            msgBox.Controls.Add(lblWidth);

            msgBox.Controls.Add(txtHeight);
            msgBox.Controls.Add(lblHeight);

            msgBox.Width = 200;

            txtWidth.Left = msgBox.Width - txtWidth.Width - 25;
            txtWidth.Top = 15;
            lblHeight.Width = lblWidth.Width = 55;
            lblWidth.Left = txtWidth.Left - lblWidth.Width - 5; ; lblWidth.Top = txtWidth.Top;

            lblHeight.Left = lblWidth.Left; lblHeight.Top = lblWidth.Top + 35;
            txtHeight.Top = lblHeight.Top; txtHeight.Left = txtWidth.Left;

            msgBox.btns[0].Top = msgBox.btns[1].Top = txtHeight.Top + txtHeight.Height + 15;
            msgBox.btns[1].Left = msgBox.Width - msgBox.btns[1].Width - 25;

            msgBox.btns[0].Left = msgBox.btns[1].Left - msgBox.btns[0].Width - 10;

            msgBox.Height = msgBox.btns[0].Top + msgBox.btns[0].Height + 45;

            txtWidth.SelectAll();
            txtWidth.Focus();

            msgBox.Owner = callingForm;
            msgBox.ShowDialog();
            int intNewWidth = 0;
            int intNewHeight = 0;

            try { intNewWidth = Convert.ToInt16(txtWidth.Text); }
            catch (Exception) { return; }
            try { intNewHeight = Convert.ToInt16(txtHeight.Text); }
            catch (Exception) { return; }
            if (msgBox.Reply != null && msgBox.Reply.CompareTo(strOk) == 0)
            {
                // resize image
                Bitmap bmpNew = new Bitmap(intNewWidth, intNewHeight);
                using (Graphics g = Graphics.FromImage(bmpNew))
                {
                    g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, bmpNew.Width, bmpNew.Height));
                    Rectangle recDest = new Rectangle(0, 0, bmpNew.Width, bmpNew.Height);
                    Rectangle recSrc = new Rectangle(0, 0, bmpNew.Width, bmpNew.Height);
                    g.DrawImage(bmp, recDest, recSrc, GraphicsUnit.Pixel);
                }
                pic.Image = new Bitmap(bmpNew);
                bmp = new Bitmap(bmpNew);
                udrSelect.bmp.bmpBackground = new Bitmap(bmp);
                if (intZoomFactor > 1)
                {
                    int intWidth = bmp.Width * intPixelSize;
                    int intHeight = bmp.Height * intPixelSize;
                    Bitmap bmpNewZoom = new Bitmap(intWidth, intHeight);
                    using (Graphics g = Graphics.FromImage(bmpNewZoom))
                    {
                        g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, intWidth, intHeight));

                        Rectangle recDest = new Rectangle(0, 0, bmpNewZoom.Width, bmpNewZoom.Height);
                        Rectangle recSrc = new Rectangle(0, 0, bmpNewZoom.Width, bmpNewZoom.Height);
                        g.DrawImage(bmpZoomed, recDest, recSrc, GraphicsUnit.Pixel);
                    }
                    bmpZoomed = new Bitmap(bmpNewZoom);
                    udrSelect.bmpZoomed.bmpBackground = new Bitmap(bmpZoomed);
                    pic.Image = bmpZoomed;
                }
                placeObjectsOnPanel();
                backupImage();
            }
            msgBox.Dispose();
        }

        private void mnuFile_Resize_Click(object sender, EventArgs e)
        {
            classMyResizeFileForm frmResize = new classMyResizeFileForm();
            frmResize.Owner = callingForm;
            frmResize.ShowDialog();
            frmResize.Dispose();
        }

        private void mnuFile_Load_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Bitmaps|*.bmp";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                PictureBox picBox = new PictureBox();

                picBox.Load(ofd.FileName);

                Bitmap bmpNew = new Bitmap((Bitmap)picBox.Image);
                setImage(bmpNew);
            }
        }

        private void mnuFile_New_Click(object sender, EventArgs e)
        {
            if (bmp == null)
                udrSelect.bmp.bmpBackground = bmp = new Bitmap(200, 300);
            else
                udrSelect.bmp.bmpBackground = bmp = new Bitmap(bmp.Width, bmp.Height);

            using (Graphics g = Graphics.FromImage(bmp))
                g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, bmp.Width, bmp.Height));

            udrSelect.bmpZoomed.bmpBackground = bmpZoomed = null;

            udrSelect.bmp.bmpSelected = null;
            udrSelect.bmpZoomed.bmpSelected = null;

            pic.Image = bmp;
            setZoomFactor(1);
            pic.Refresh();

            placeObjectsOnPanel();

            initSelectVars();
        }

        private void mnuFile_NewWindow_Click(object sender, EventArgs e)
        {
            Form frmNew = new Form();
            classGraphicsPicEditor cLibEditNew = new classGraphicsPicEditor(frmNew);
            frmNew.Controls.Add(cLibEditNew);
            cLibEditNew.Dock = DockStyle.Fill;
            cLibEditNew.Visible = true;
            frmNew.Show();
        }

        private void mnuFile_Save_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                string strFilename = sfd.FileName;
                int intCutChr = strFilename.IndexOf(".");
                if (intCutChr > 1)
                    strFilename = (strFilename.Substring(0, intCutChr)).Replace(".", "");

                strFilename += ".bmp";
                bmp.Save(strFilename);
            }
        }

        private void floodfill_sameColor(Point ptSeed)
        {
            picQ.head = null;
            picQ.tail = null;
            if (ptSeed.X >= bmp.Width || ptSeed.Y >= bmp.Height)
                return;
            Color clrOriginal = bmp.GetPixel(ptSeed.X, ptSeed.Y);

            classPixelPos seed = new classPixelPos();
            seed.pos.X = ptSeed.X; seed.pos.Y = ptSeed.Y;

            if (bmp == null) return;

            enQ(ref picQ, ref seed);

            bool[,] bolSeenMap = new bool[bmp.Width, bmp.Height];

            while (picQ.head != null)
            {
                classPixelPos pixel = deQ(ref picQ);
                Color clrPixel = bmp.GetPixel(pixel.pos.X, pixel.pos.Y);

                if (clrPixel != clrFloodFill_set)
                    bmp.SetPixel(pixel.pos.X, pixel.pos.Y, clrFloodFill_set);

                for (enuDirections dircounter = 0; dircounter < enuDirections._numDir; dircounter++)
                {
                    Point ptNext = move(pixel.pos, dircounter);
                    if (ptNext.X >= 0 && ptNext.X < bmp.Width)
                        if (ptNext.Y >= 0 && ptNext.Y < bmp.Height)
                        {
                            if (!bolSeenMap[ptNext.X, ptNext.Y])
                            {
                                Color clrPix = bmp.GetPixel(ptNext.X, ptNext.Y);
                                if (clrPix == clrOriginal)
                                {
                                    classPixelPos pixelNew = new classPixelPos();
                                    pixelNew.pos = ptNext;
                                    enQ(ref picQ, ref pixelNew);
                                }
                            }
                            bolSeenMap[ptNext.X, ptNext.Y] = true;
                        }
                }
            }
            if (intZoomFactor == 1)
                pic.Image = bmp;
            else
            {
                resizeImage();

            }
            pic.Refresh();
            backupImage();
        }

        private void floodfill_toBorder(Point ptSeed)
        {
            picQ.head = null;
            picQ.tail = null;

            classPixelPos seed = new classPixelPos();
            seed.pos.X = ptSeed.X; seed.pos.Y = ptSeed.Y;
            if (bmp == null)
                return;
            enQ(ref picQ, ref seed);

            bool[,] bolSeenMap = new bool[bmp.Width, bmp.Height];

            while (picQ.head != null)
            {
                classPixelPos pixel = deQ(ref picQ);
                Color clrPixel = bmp.GetPixel(pixel.pos.X, pixel.pos.Y);

                if (clrPixel != clrFloodFill_set && clrPixel != clrFloodFill_border)
                    bmp.SetPixel(pixel.pos.X, pixel.pos.Y, clrFloodFill_set);

                for (enuDirections dircounter = 0; dircounter < enuDirections._numDir; dircounter++)
                {
                    Point ptNext = move(pixel.pos, dircounter);
                    if (ptNext.X >= 0 && ptNext.X < bmp.Width)
                        if (ptNext.Y >= 0 && ptNext.Y < bmp.Height)
                        {
                            if (!bolSeenMap[ptNext.X, ptNext.Y])
                            {
                                Color clrPix = bmp.GetPixel(ptNext.X, ptNext.Y);
                                if (clrPix != clrFloodFill_border
                                    && clrPix != clrFloodFill_set)
                                {
                                    classPixelPos pixelNew = new classPixelPos();
                                    pixelNew.pos = ptNext;
                                    enQ(ref picQ, ref pixelNew);
                                }
                            }
                            bolSeenMap[ptNext.X, ptNext.Y] = true;
                        }
                }
            }
            if (intZoomFactor == 1)
                pic.Image = bmp;
            else
            {
                resizeImage();

            }
            pic.Refresh();
            backupImage();
        }
    }

    public class formFlipRotate : Form
    {
        public enum enuResponse { flipH, flipV, R90, R180, R270, cancel }
        public enuResponse reply;

        Button btnCancel = new Button();
        Button btnOk = new Button();

        classMyFlipRotateRadioButton rdbFlipHorizontal = new classMyFlipRotateRadioButton(enuResponse.flipH);
        classMyFlipRotateRadioButton rdbFlipVertical = new classMyFlipRotateRadioButton(enuResponse.flipV);

        classMyFlipRotateRadioButton rdbRotate = new classMyFlipRotateRadioButton(enuResponse.R90);
        classMyFlipRotateRadioButton rdbRotate90 = new classMyFlipRotateRadioButton(enuResponse.R90);
        classMyFlipRotateRadioButton rdbRotate180 = new classMyFlipRotateRadioButton(enuResponse.R180);
        classMyFlipRotateRadioButton rdbRotate270 = new classMyFlipRotateRadioButton(enuResponse.R270);

        Panel pnlRotate = new Panel();

        public formFlipRotate()
        {
            Text = "Flip and Rotate";
            int intVSpaceBWRdB = 18;

            rdbRotate.Checked = true;
            rdbRotate90.Checked = true;

            GroupBox grbFlipRotate = new GroupBox();
            Controls.Add(grbFlipRotate);
            grbFlipRotate.Top = 10;
            grbFlipRotate.Left = 5;
            grbFlipRotate.Text = "Flip or Rotate";

            Panel pnlFlipRotate = new Panel();
            grbFlipRotate.Controls.Add(pnlFlipRotate);

            rdbFlipHorizontal.Text = "&Flip Horizontal";
            pnlFlipRotate.Controls.Add(rdbFlipHorizontal);
            rdbFlipHorizontal.Left = 5;
            rdbFlipHorizontal.Top = 1;
            int intMinMainPanelWidth = rdbFlipHorizontal.Width;

            rdbFlipVertical.Text = "Flip &vertical";
            pnlFlipRotate.Controls.Add(rdbFlipVertical);
            rdbFlipVertical.Left = rdbFlipHorizontal.Left;
            rdbFlipVertical.Top = rdbFlipHorizontal.Top + intVSpaceBWRdB;
            intMinMainPanelWidth = (intMinMainPanelWidth > rdbFlipVertical.Width) ? intMinMainPanelWidth : rdbFlipVertical.Width;

            rdbRotate.Text = "&Rotate by angle";
            pnlFlipRotate.Controls.Add(rdbRotate);
            rdbRotate.Left = rdbFlipHorizontal.Left;
            rdbRotate.Top = rdbFlipVertical.Top + intVSpaceBWRdB;
            intMinMainPanelWidth = (intMinMainPanelWidth > rdbRotate.Width) ? intMinMainPanelWidth : rdbRotate.Width;

            pnlFlipRotate.Width = intMinMainPanelWidth + 5;
            pnlFlipRotate.Height = rdbRotate.Top + intVSpaceBWRdB;

            pnlFlipRotate.Top = 15;
            pnlFlipRotate.Left = 5;

            grbFlipRotate.Width = pnlFlipRotate.Left + pnlFlipRotate.Width + 15;

            grbFlipRotate.Controls.Add(pnlRotate);

            rdbRotate90.Text = "&90";// +Convert.ToChar(168);
            pnlRotate.Controls.Add(rdbRotate90);
            rdbRotate90.Top = 1;
            rdbRotate90.Left = 5;
            int intMinRotatePanelWidth = rdbRotate90.Width;

            rdbRotate180.Text = "&180";// + Convert.ToChar(169);
            pnlRotate.Controls.Add(rdbRotate180);
            rdbRotate180.Left = rdbRotate90.Left;
            rdbRotate180.Top = rdbRotate90.Top + intVSpaceBWRdB;
            intMinRotatePanelWidth = (intMinRotatePanelWidth > rdbRotate180.Width) ? intMinRotatePanelWidth : rdbRotate180.Width;

            rdbRotate270.Text = "&270";//+ Convert.ToChar(170);
            pnlRotate.Controls.Add(rdbRotate270);
            rdbRotate270.Left = rdbRotate90.Left;
            rdbRotate270.Top = rdbRotate180.Top + intVSpaceBWRdB;
            intMinRotatePanelWidth = (intMinRotatePanelWidth > rdbRotate270.Width) ? intMinRotatePanelWidth : rdbRotate270.Width;

            pnlRotate.Width = intMinRotatePanelWidth;
            pnlRotate.Height = rdbRotate270.Top + rdbRotate270.Height;

            pnlRotate.Top = pnlFlipRotate.Top + pnlFlipRotate.Height;
            pnlRotate.Left = pnlFlipRotate.Left + 60;

            grbFlipRotate.Height = 150;
            grbFlipRotate.Width = 190;

            rdbFlipHorizontal.CheckedChanged += new EventHandler(rdb_CheckedChanged);
            rdbFlipVertical.CheckedChanged += new EventHandler(rdb_CheckedChanged);

            rdbRotate.CheckedChanged += new EventHandler(rdbRotate_CheckedChanged);

            rdbRotate90.CheckedChanged += new EventHandler(rdb_CheckedChanged);
            rdbRotate180.CheckedChanged += new EventHandler(rdb_CheckedChanged);
            rdbRotate270.CheckedChanged += new EventHandler(rdb_CheckedChanged);

            Controls.Add(btnOk);
            btnOk.Text = "&Ok";
            btnOk.Left = grbFlipRotate.Left + grbFlipRotate.Width + 15;
            btnOk.Top = grbFlipRotate.Top;
            btnOk.Click += new EventHandler(btnOk_Click);

            Controls.Add(btnCancel);
            btnCancel.Text = "&Cancel";
            btnCancel.Left = btnOk.Left;
            btnCancel.Top = btnOk.Top + btnOk.Height + 5;
            btnCancel.Click += new EventHandler(btnCancel_Click);

            Width = btnCancel.Left + btnCancel.Width + 25;
            Height = grbFlipRotate.Top + grbFlipRotate.Height + 45;

            reply = enuResponse.flipH;
            rdbFlipHorizontal.Checked = true;
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            reply = enuResponse.cancel;
            Dispose();
        }

        void btnOk_Click(object sender, EventArgs e)
        {
            Dispose();
        }

        void rdbRotate_CheckedChanged(object sender, EventArgs e)
        {
            rdbRotate90.Checked = true;
            pnlRotate.Enabled = rdbRotate.Checked;
            reply = enuResponse.R90;
        }

        void rdb_CheckedChanged(object sender, EventArgs e)
        {
            classMyFlipRotateRadioButton rdbThis = (classMyFlipRotateRadioButton)sender;
            if (rdbThis.Checked)
                reply = rdbThis.myResponseValue;
            btnOk.Focus();
        }

    }

    public class classMyFlipRotateRadioButton : RadioButton
    {
        public formFlipRotate.enuResponse myResponseValue;
        public classMyFlipRotateRadioButton(formFlipRotate.enuResponse MyResponseValue)
        {
            myResponseValue = MyResponseValue;
        }
    }

    public class classMyResizeFileForm : Form
    {
        string[] strFilesInput;
        double dblFactor = 1.0;

        HScrollBar hsb = new HScrollBar();
        Label lbl = new Label();
        TextBox txt = new TextBox();
        Button btnOk = new Button();
        Button btnCancel = new Button();
        TextBox txtAppend = new TextBox();
        ListBox lst = new ListBox();

        public classMyResizeFileForm()
        {
            Controls.Add(lst);
            lst.Left = 5;
            lst.Top = 5;
            lst.Height = 250;
            lst.Click += new EventHandler(lst_Click);

            Controls.Add(lbl);
            lbl.Text = "Set desired ratio : %";
            lbl.Top = lst.Top;
            lbl.Left = lst.Left + lst.Width + 5;
            lbl.AutoSize = true;
            lbl.Font = new Font("ms sans-serif", 14);

            Controls.Add(txt);
            txt.Text =(dblFactor*100).ToString("f1");
            txt.Left = lbl.Left + lbl.Width + 5;
            txt.Top = lbl.Top;
            txt.Width = 65;
            txt.Font = lbl.Font;

            Controls.Add(btnOk);
            btnOk.Text = "&Ok";
            btnOk.AutoSize = true;
            btnOk.Font = lbl.Font;
            btnOk.Left = txt.Left + txt.Width + 5;
            btnOk.Top = lbl.Top;
            btnOk.Click += new EventHandler(btnOk_Click);

            Controls.Add(btnCancel);
            btnCancel.Text = "&Cancel";
            btnCancel.AutoSize = true;
            btnCancel.Left = btnOk.Left + btnOk.Width + 5;
            btnCancel.Top = btnOk.Top;
            btnCancel.Click += new EventHandler(btnCancel_Click);

            Controls.Add(txtAppend);
            txtAppend.Left = lbl.Left;
            txtAppend.Top = lbl.Top + lbl.Height + 5;
            txtAppend.Text = "_resized";

            Controls.Add(hsb);
            hsb.Left = lbl.Left;
            hsb.Top = txtAppend.Top + txtAppend.Height + 5;
            hsb.Width = btnCancel.Left + btnCancel.Width  - lbl.Left;
            hsb.Minimum = 10;
            hsb.Maximum = 3000;
            hsb.Value = 1000;
            hsb.ValueChanged += new EventHandler(hsb_ValueChanged);

            Height = lst.Height + 35;
            Width = hsb.Width + lst.Width + 35;
        }

        void lst_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = true;
            if (ofd.ShowDialog() == DialogResult.OK)
                strFilesInput = ofd.FileNames;
            lst.Items.Clear();
            for (int intFileCounter = 0; intFileCounter < strFilesInput.Length; intFileCounter++)
                lst.Items.Add(strFilesInput[intFileCounter]);
        }

        void hsb_ValueChanged(object sender, EventArgs e)
        {
            txt.Text = (((double)hsb.Value)/10).ToString("f1");
        }


        void btnOk_Click(object sender, EventArgs e)
        {   
            try
            {
                dblFactor = Convert.ToDouble(txt.Text.ToString())/100;
            }
            catch (Exception)
            {
                invalidFactorSize();
                return;
            }

            if (dblFactor < 0 || dblFactor > 10)
            {
                invalidFactorSize();
                return;
            }

            PictureBox pic = new PictureBox();
            pic.SizeMode = PictureBoxSizeMode.AutoSize;

            for (int intFileCounter = 0; intFileCounter < strFilesInput.Length; intFileCounter++)
            {
                pic.Load(strFilesInput[intFileCounter]);
                Bitmap bmpTemp = new Bitmap(pic.Image, new Size((int)(pic.Image.Width * dblFactor),(int)( pic.Image.Height * dblFactor)));
                string strFilename = strFilesInput[intFileCounter];
                int intCutChr = strFilename.IndexOf(".");
                if (intCutChr >1)
                strFilename = strFilename.Substring(0, intCutChr)  + txtAppend.Text.ToString().Trim()+ ".bmp";
                bmpTemp.Save(strFilename);
            }
            Dispose();
        }

        void invalidFactorSize()
        {
            MessageBox.Show("invalid factor size", "error");
            txt.SelectAll();
            txt.Focus();
        }
        
        void btnCancel_Click(object sender, EventArgs e) { Dispose(); }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

Christ Kennedy
CEO unemployable
Canada Canada
Christ Kennedy, published his fourth novel "Cleats of the Counter Revolution" in the summer of 2010. He grew up in the suburbs of Montreal and is a bilingual Quebecois with a bachelor’s degree in computer engineering from McGill University and is currently walking across ontario plotting a new novel, far away from any computer.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 27 Jan 2010
Article Copyright 2010 by Christ Kennedy
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid