2013-11-03 23:53:49 +13:00
#region License Information ( GPL v3 )
/ *
ShareX - A program that allows you to take screenshots and share any file type
2014-12-31 22:41:32 +13:00
Copyright © 2007 - 2015 ShareX Developers
2013-11-03 23:53:49 +13:00
This program is free software ; you can redistribute it and / or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation ; either version 2
of the License , or ( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program ; if not , write to the Free Software
Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA .
Optionally you can also view the license at < http : //www.gnu.org/licenses/>.
* /
#endregion License Information ( GPL v3 )
2014-12-11 09:25:20 +13:00
using ShareX.HelpersLib ;
using ShareX.ScreenCaptureLib.Properties ;
2014-08-08 18:44:18 +12:00
using System ;
2013-11-03 23:53:49 +13:00
using System.Collections.Generic ;
using System.Drawing ;
using System.Drawing.Drawing2D ;
2015-05-07 03:15:36 +12:00
using System.Text ;
2013-11-03 23:53:49 +13:00
using System.Windows.Forms ;
2014-12-11 09:25:20 +13:00
namespace ShareX.ScreenCaptureLib
2013-11-03 23:53:49 +13:00
{
public class RectangleRegion : Surface
{
public AreaManager AreaManager { get ; private set ; }
2015-01-25 13:22:46 +13:00
#region Screen color picker
2015-05-13 05:02:51 +12:00
public bool ScreenColorPickerMode { get ; set ; }
2013-11-03 23:53:49 +13:00
2015-01-25 13:22:46 +13:00
private Bitmap bmpSurfaceImage ;
public Point CurrentPosition { get ; set ; }
public Color CurrentColor
{
get
{
if ( bmpSurfaceImage ! = null & & ! CurrentPosition . IsEmpty )
{
return bmpSurfaceImage . GetPixel ( CurrentPosition . X , CurrentPosition . Y ) ;
}
return Color . Empty ;
}
}
#endregion Screen color picker
#region Screen ruler
2014-04-12 04:29:19 +12:00
public bool RulerMode { get ; set ; }
2015-01-25 13:22:46 +13:00
#endregion Screen ruler
2014-11-27 06:48:25 +13:00
public RectangleRegion ( )
2013-11-03 23:53:49 +13:00
{
AreaManager = new AreaManager ( this ) ;
KeyDown + = RectangleRegion_KeyDown ;
MouseDown + = RectangleRegion_MouseDown ;
MouseWheel + = RectangleRegion_MouseWheel ;
}
private void RectangleRegion_MouseDown ( object sender , MouseEventArgs e )
{
2015-05-13 05:02:51 +12:00
if ( ScreenColorPickerMode & & e . Button = = MouseButtons . Left )
2013-11-03 23:53:49 +13:00
{
2015-01-25 13:22:46 +13:00
CurrentPosition = e . Location ;
2013-11-03 23:53:49 +13:00
Close ( SurfaceResult . Region ) ;
}
}
private void RectangleRegion_KeyDown ( object sender , KeyEventArgs e )
{
switch ( e . KeyData )
{
2015-05-07 03:15:36 +12:00
case Keys . F1 :
Config . ShowTips = ! Config . ShowTips ;
break ;
2013-11-03 23:53:49 +13:00
case Keys . I :
Config . ShowInfo = ! Config . ShowInfo ;
break ;
case Keys . C :
Config . ShowCrosshair = ! Config . ShowCrosshair ;
break ;
case Keys . M :
Config . ShowMagnifier = ! Config . ShowMagnifier ;
break ;
2014-04-30 01:40:36 +12:00
case Keys . Control | Keys . C :
CopyAreaInfo ( ) ;
break ;
}
}
private void CopyAreaInfo ( )
{
if ( AreaManager . IsCurrentAreaValid )
{
2015-05-10 04:59:19 +12:00
string clipboardText = GetAreaText ( AreaManager . CurrentArea ) ;
2014-04-30 01:40:36 +12:00
ClipboardHelpers . CopyText ( clipboardText ) ;
2013-11-03 23:53:49 +13:00
}
}
private void RectangleRegion_MouseWheel ( object sender , MouseEventArgs e )
{
if ( e . Delta > 0 )
{
if ( ModifierKeys . HasFlag ( Keys . Control ) )
{
if ( Config . MagnifierPixelSize < 30 ) Config . MagnifierPixelSize + + ;
}
else
{
2014-07-25 13:14:48 +12:00
if ( Config . MagnifierPixelCount < 41 ) Config . MagnifierPixelCount + = 2 ;
2013-11-03 23:53:49 +13:00
}
}
else if ( e . Delta < 0 )
{
if ( ModifierKeys . HasFlag ( Keys . Control ) )
{
2014-07-25 13:14:48 +12:00
if ( Config . MagnifierPixelSize > 2 ) Config . MagnifierPixelSize - - ;
2013-11-03 23:53:49 +13:00
}
else
{
2014-07-25 13:14:48 +12:00
if ( Config . MagnifierPixelCount > 2 ) Config . MagnifierPixelCount - = 2 ;
2013-11-03 23:53:49 +13:00
}
}
}
public override void Prepare ( )
{
base . Prepare ( ) ;
if ( Config ! = null )
{
AreaManager . WindowCaptureMode | = Config . ForceWindowCapture ;
2014-07-10 09:44:08 +12:00
AreaManager . IncludeControls | = Config . IncludeControls ;
2013-11-03 23:53:49 +13:00
if ( AreaManager . WindowCaptureMode )
{
2014-08-08 18:44:18 +12:00
IntPtr handle = Handle ;
TaskEx . Run ( ( ) = >
{
2014-09-13 19:36:16 +12:00
WindowsRectangleList wla = new WindowsRectangleList ( ) ;
2014-08-08 18:44:18 +12:00
wla . IgnoreHandle = handle ;
wla . IncludeChildWindows = AreaManager . IncludeControls ;
AreaManager . Windows = wla . GetWindowsRectangleList ( ) ;
} ) ;
2013-11-03 23:53:49 +13:00
}
}
2015-01-25 13:22:46 +13:00
2015-05-13 05:02:51 +12:00
if ( ScreenColorPickerMode )
2015-01-25 13:22:46 +13:00
{
bmpSurfaceImage = new Bitmap ( SurfaceImage ) ;
}
2013-11-03 23:53:49 +13:00
}
protected override void Update ( )
{
base . Update ( ) ;
AreaManager . Update ( ) ;
}
protected override void Draw ( Graphics g )
{
List < Rectangle > areas = AreaManager . GetValidAreas ;
if ( areas . Count > 0 | | ! AreaManager . CurrentHoverArea . IsEmpty )
{
UpdateRegionPath ( ) ;
if ( areas . Count > 0 )
{
2014-11-18 01:55:20 +13:00
if ( Config . UseDimming )
2013-11-03 23:53:49 +13:00
{
2014-11-18 01:55:20 +13:00
using ( Region region = new Region ( regionDrawPath ) )
{
g . Clip = region ;
g . FillRectangle ( lightBackgroundBrush , ScreenRectangle0Based ) ;
g . ResetClip ( ) ;
}
2013-11-03 23:53:49 +13:00
}
g . DrawPath ( borderPen , regionDrawPath ) ;
2015-05-08 06:25:23 +12:00
/ *
2013-11-03 23:53:49 +13:00
if ( areas . Count > 1 )
{
Rectangle totalArea = AreaManager . CombineAreas ( ) ;
g . DrawCrossRectangle ( borderPen , totalArea , 15 ) ;
if ( Config . ShowInfo )
{
2014-04-12 04:29:19 +12:00
ImageHelpers . DrawTextWithOutline ( g , string . Format ( "X: {0} / Y: {1} / W: {2} / H: {3}" , totalArea . X , totalArea . Y ,
2013-11-03 23:53:49 +13:00
totalArea . Width , totalArea . Height ) , new PointF ( totalArea . X + 5 , totalArea . Y - 25 ) , textFont , Color . White , Color . Black ) ;
}
}
2015-05-08 06:25:23 +12:00
* /
2013-11-03 23:53:49 +13:00
}
if ( AreaManager . IsCurrentHoverAreaValid )
{
using ( GraphicsPath hoverDrawPath = new GraphicsPath { FillMode = FillMode . Winding } )
{
AddShapePath ( hoverDrawPath , AreaManager . CurrentHoverArea . SizeOffset ( - 1 ) ) ;
2014-07-25 17:54:24 +12:00
g . DrawPath ( borderPen , hoverDrawPath ) ;
2013-11-03 23:53:49 +13:00
g . DrawPath ( borderDotPen , hoverDrawPath ) ;
}
}
if ( AreaManager . IsCurrentAreaValid )
{
2014-07-25 17:54:24 +12:00
g . DrawRectangleProper ( borderPen , AreaManager . CurrentArea ) ;
2013-11-03 23:53:49 +13:00
g . DrawRectangleProper ( borderDotPen , AreaManager . CurrentArea ) ;
DrawObjects ( g ) ;
2014-04-12 04:29:19 +12:00
if ( RulerMode )
{
DrawRuler ( g , AreaManager . CurrentArea , borderPen , 5 , 10 ) ;
DrawRuler ( g , AreaManager . CurrentArea , borderPen , 15 , 100 ) ;
2014-04-12 05:12:13 +12:00
Point centerPos = new Point ( AreaManager . CurrentArea . X + AreaManager . CurrentArea . Width / 2 , AreaManager . CurrentArea . Y + AreaManager . CurrentArea . Height / 2 ) ;
int markSize = 10 ;
g . DrawLine ( borderPen , centerPos . X , centerPos . Y - markSize , centerPos . X , centerPos . Y + markSize ) ;
g . DrawLine ( borderPen , centerPos . X - markSize , centerPos . Y , centerPos . X + markSize , centerPos . Y ) ;
2014-04-12 04:29:19 +12:00
}
2013-11-03 23:53:49 +13:00
}
if ( Config . ShowInfo )
{
foreach ( Rectangle area in areas )
{
if ( area . IsValid ( ) )
{
2015-05-10 04:59:19 +12:00
string areaText = GetAreaText ( area ) ;
DrawAreaText ( g , areaText , area ) ;
2013-11-03 23:53:49 +13:00
}
}
}
}
2015-05-07 03:15:36 +12:00
if ( Config . ShowTips )
{
DrawTips ( g , 10 , 10 ) ;
}
2013-11-03 23:53:49 +13:00
if ( Config . ShowMagnifier )
{
DrawMagnifier ( g ) ;
}
if ( Config . ShowCrosshair )
{
DrawCrosshair ( g ) ;
}
}
2015-05-13 04:27:21 +12:00
private void DrawInfoText ( Graphics g , string text , Rectangle rect , int padding )
{
g . FillRectangle ( textBackgroundBrush , rect . Offset ( - 2 ) ) ;
g . DrawRectangleProper ( textBackgroundPenBlack , rect . Offset ( - 1 ) ) ;
g . DrawRectangleProper ( textBackgroundPenWhite , rect ) ;
ImageHelpers . DrawTextWithShadow ( g , text , rect . Offset ( - padding ) . Location , infoFont , Brushes . White , Brushes . Black ) ;
}
2015-05-10 04:59:19 +12:00
private void DrawAreaText ( Graphics g , string text , Rectangle area )
2015-05-08 11:31:08 +12:00
{
int offset = 5 ;
2015-05-13 04:27:21 +12:00
int backgroundPadding = 3 ;
2015-05-10 04:59:19 +12:00
Size textSize = g . MeasureString ( text , infoFont ) . ToSize ( ) ;
2015-05-08 11:31:08 +12:00
Point textPos ;
2015-05-13 04:27:21 +12:00
if ( area . Y - offset - textSize . Height - backgroundPadding * 2 < ScreenRectangle0Based . Y )
2015-05-08 11:31:08 +12:00
{
2015-05-13 04:27:21 +12:00
textPos = new Point ( area . X + offset + backgroundPadding , area . Y + offset + backgroundPadding ) ;
2015-05-08 11:31:08 +12:00
}
else
{
2015-05-13 04:27:21 +12:00
textPos = new Point ( area . X + backgroundPadding , area . Y - offset - backgroundPadding - textSize . Height ) ;
2015-05-08 11:31:08 +12:00
}
2015-05-13 04:27:21 +12:00
Rectangle backgroundRect = new Rectangle ( textPos . X - backgroundPadding , textPos . Y - backgroundPadding , textSize . Width + backgroundPadding * 2 , textSize . Height + backgroundPadding * 2 ) ;
2015-05-08 11:31:08 +12:00
2015-05-13 04:27:21 +12:00
DrawInfoText ( g , text , backgroundRect , backgroundPadding ) ;
2015-05-08 11:31:08 +12:00
}
2015-05-07 03:15:36 +12:00
private void DrawTips ( Graphics g , int offset , int padding )
{
StringBuilder sb = new StringBuilder ( ) ;
2015-05-07 05:14:45 +12:00
WriteTips ( sb ) ;
string tipText = sb . ToString ( ) . Trim ( ) ;
2015-05-08 11:31:08 +12:00
Size textSize = g . MeasureString ( tipText , infoFont ) . ToSize ( ) ;
2015-05-07 05:14:45 +12:00
int rectWidth = textSize . Width + padding * 2 + 2 ;
int rectHeight = textSize . Height + padding * 2 ;
Rectangle primaryScreenBounds = CaptureHelpers . GetPrimaryScreenBounds0Based ( ) ;
Rectangle textRectangle = new Rectangle ( primaryScreenBounds . X + primaryScreenBounds . Width - rectWidth - offset , primaryScreenBounds . Y + offset , rectWidth , rectHeight ) ;
if ( textRectangle . Offset ( 10 ) . Contains ( InputManager . MousePosition0Based ) )
{
textRectangle . Y = primaryScreenBounds . Height - rectHeight - offset ;
}
2015-05-13 04:27:21 +12:00
DrawInfoText ( g , tipText , textRectangle , padding ) ;
2015-05-07 05:14:45 +12:00
}
protected virtual void WriteTips ( StringBuilder sb )
{
2015-05-08 02:32:48 +12:00
sb . AppendLine ( "[F1] Hide tips" ) ;
2015-05-07 03:15:36 +12:00
sb . AppendLine ( ) ;
if ( AreaManager . IsCreating )
{
sb . AppendLine ( "[Right click] Cancel region selection" ) ;
2015-05-07 05:14:45 +12:00
sb . AppendLine ( "[Esc] Cancel capture" ) ;
2015-05-07 03:15:36 +12:00
}
else
{
sb . AppendLine ( "[Hold Left click] Start region selection" ) ;
2015-05-07 05:14:45 +12:00
sb . AppendLine ( "[Right click] [Esc] Cancel capture" ) ;
2015-05-07 03:15:36 +12:00
}
2015-05-07 04:17:58 +12:00
if ( ! Config . QuickCrop & & AreaManager . Areas . Count > 0 )
{
sb . AppendLine ( "[Double Left click] [Enter] Capture regions" ) ;
}
sb . AppendLine ( ) ;
if ( ! Config . QuickCrop & & ! AreaManager . IsCreating & & AreaManager . IsCurrentAreaValid )
{
sb . AppendLine ( string . Format ( "[Arrow keys] Resize selected region from {0}" , AreaManager . ResizeManager . IsBottomRightResizing ? "bottom right" : "top left" ) ) ;
sb . AppendLine ( string . Format ( "[Tab] Swap resize anchor to {0}" , AreaManager . ResizeManager . IsBottomRightResizing ? "top left" : "bottom right" ) ) ;
sb . AppendLine ( "[Hold Shift] Move selected region instead resizing" ) ;
sb . AppendLine ( "[Hold Ctrl] Resize / Move faster" ) ;
sb . AppendLine ( "[Hold Left click on selection] Move region" ) ;
sb . AppendLine ( "[Right click on selection] [Delete] Remove region" ) ;
}
else
{
sb . AppendLine ( "[Arrow keys] Move cursor position" ) ;
sb . AppendLine ( "[Ctrl + Arrow keys] Move cursor position faster" ) ;
}
2015-05-07 03:15:36 +12:00
if ( AreaManager . IsCreating )
{
sb . AppendLine ( "[Hold Shift] Proportional resizing" ) ;
2015-05-07 04:17:58 +12:00
}
if ( AreaManager . IsCurrentAreaValid )
{
2015-05-07 03:15:36 +12:00
sb . AppendLine ( "[Ctrl + C] Copy position and size" ) ;
}
sb . AppendLine ( ) ;
sb . AppendLine ( "[Space] Fullscreen capture" ) ;
sb . AppendLine ( "[1, 2, 3 ... 0] Monitor capture" ) ;
2015-05-07 04:42:13 +12:00
sb . AppendLine ( "[~] Active monitor capture" ) ;
2015-05-07 03:15:36 +12:00
sb . AppendLine ( ) ;
if ( Config . QuickCrop )
{
sb . AppendLine ( "[Q] Multi region mode" ) ;
}
else
{
sb . AppendLine ( "[Q] Quick capture mode" ) ;
}
sb . AppendLine ( "[Mouse wheel] Change magnifier pixel count" ) ;
sb . AppendLine ( "[Ctrl + Mouse wheel] Change magnifier pixel size" ) ;
2015-05-10 04:59:19 +12:00
sb . AppendLine ( string . Format ( "[I] {0} position and size info" , Config . ShowInfo ? "Hide" : "Show" ) ) ;
2015-05-07 03:15:36 +12:00
sb . AppendLine ( string . Format ( "[M] {0} magnifier" , Config . ShowMagnifier ? "Hide" : "Show" ) ) ;
sb . AppendLine ( string . Format ( "[C] {0} screen wide crosshair" , Config . ShowCrosshair ? "Hide" : "Show" ) ) ;
}
2014-04-30 01:40:36 +12:00
private string GetAreaText ( Rectangle area )
{
2015-05-10 04:59:19 +12:00
if ( RulerMode )
{
Point endPos = new Point ( area . Right - 1 , area . Bottom - 1 ) ;
return string . Format ( Resources . RectangleRegion_GetRulerText_Ruler_info , area . X , area . Y , endPos . X , endPos . Y ,
area . Width , area . Height , MathHelpers . Distance ( area . Location , endPos ) , MathHelpers . LookAtDegree ( area . Location , endPos ) ) ;
}
2014-10-18 00:08:33 +13:00
return string . Format ( Resources . RectangleRegion_GetAreaText_Area , area . X , area . Y , area . Width , area . Height ) ;
2014-04-30 01:40:36 +12:00
}
2013-11-03 23:53:49 +13:00
private void DrawCrosshair ( Graphics g )
{
2014-07-25 17:54:24 +12:00
int offset = 5 ;
2013-11-03 23:53:49 +13:00
Point mousePos = InputManager . MousePosition0Based ;
Point left = new Point ( mousePos . X - offset , mousePos . Y ) , left2 = new Point ( 0 , mousePos . Y ) ;
Point right = new Point ( mousePos . X + offset , mousePos . Y ) , right2 = new Point ( ScreenRectangle0Based . Width - 1 , mousePos . Y ) ;
Point top = new Point ( mousePos . X , mousePos . Y - offset ) , top2 = new Point ( mousePos . X , 0 ) ;
Point bottom = new Point ( mousePos . X , mousePos . Y + offset ) , bottom2 = new Point ( mousePos . X , ScreenRectangle0Based . Height - 1 ) ;
if ( left . X - left2 . X > 10 )
{
2014-07-25 17:54:24 +12:00
g . DrawLine ( borderPen , left , left2 ) ;
2013-11-03 23:53:49 +13:00
g . DrawLine ( borderDotPen , left , left2 ) ;
}
if ( right2 . X - right . X > 10 )
{
2014-07-25 17:54:24 +12:00
g . DrawLine ( borderPen , right , right2 ) ;
2013-11-03 23:53:49 +13:00
g . DrawLine ( borderDotPen , right , right2 ) ;
}
if ( top . Y - top2 . Y > 10 )
{
2014-07-25 17:54:24 +12:00
g . DrawLine ( borderPen , top , top2 ) ;
2013-11-03 23:53:49 +13:00
g . DrawLine ( borderDotPen , top , top2 ) ;
}
if ( bottom2 . Y - bottom . Y > 10 )
{
2014-07-25 17:54:24 +12:00
g . DrawLine ( borderPen , bottom , bottom2 ) ;
2013-11-03 23:53:49 +13:00
g . DrawLine ( borderDotPen , bottom , bottom2 ) ;
}
}
private void DrawMagnifier ( Graphics g )
{
Point mousePos = InputManager . MousePosition0Based ;
2014-10-20 02:37:48 +13:00
Rectangle currentScreenRect0Based = CaptureHelpers . ScreenToClient ( Screen . FromPoint ( InputManager . MousePosition ) . Bounds ) ;
2015-05-13 04:27:21 +12:00
int offsetX = 10 , offsetY = 10 , infoTextOffset = 0 , infoTextPadding = 3 ;
Rectangle infoTextRect = Rectangle . Empty ;
string infoText = string . Empty ;
if ( Config . ShowInfo )
{
infoTextOffset = 10 ;
2015-05-13 05:02:51 +12:00
CurrentPosition = InputManager . MousePosition ;
if ( ScreenColorPickerMode )
{
Color color = CurrentColor ;
infoText = string . Format ( Resources . RectangleRegion_GetColorPickerText , color . R , color . G , color . B , ColorHelpers . ColorToHex ( color ) , CurrentPosition . X , CurrentPosition . Y ) ;
}
else
{
infoText = string . Format ( "X: {0} Y: {1}" , CurrentPosition . X , CurrentPosition . Y ) ;
}
2015-05-13 04:27:21 +12:00
Size textSize = g . MeasureString ( infoText , infoFont ) . ToSize ( ) ;
infoTextRect . Size = new Size ( textSize . Width + infoTextPadding * 2 , textSize . Height + infoTextPadding * 2 ) ;
}
2013-11-03 23:53:49 +13:00
using ( Bitmap magnifier = Magnifier ( SurfaceImage , mousePos , Config . MagnifierPixelCount , Config . MagnifierPixelCount , Config . MagnifierPixelSize ) )
{
int x = mousePos . X + offsetX ;
2014-10-20 02:37:48 +13:00
if ( x + magnifier . Width > currentScreenRect0Based . Right )
2013-11-03 23:53:49 +13:00
{
x = mousePos . X - offsetX - magnifier . Width ;
}
int y = mousePos . Y + offsetY ;
2015-05-13 04:27:21 +12:00
if ( y + magnifier . Height + infoTextOffset + infoTextRect . Height > currentScreenRect0Based . Bottom )
{
y = mousePos . Y - offsetY - magnifier . Height - infoTextOffset - infoTextRect . Height ;
}
if ( Config . ShowInfo )
2013-11-03 23:53:49 +13:00
{
2015-05-13 04:27:21 +12:00
infoTextRect . Location = new Point ( x + ( magnifier . Width / 2 ) - ( infoTextRect . Width / 2 ) , y + magnifier . Height + infoTextOffset ) ;
DrawInfoText ( g , infoText , infoTextRect , 3 ) ;
2013-11-03 23:53:49 +13:00
}
2014-07-25 13:14:48 +12:00
g . SetHighQuality ( ) ;
using ( TextureBrush brush = new TextureBrush ( magnifier ) )
{
brush . TranslateTransform ( x , y ) ;
g . FillEllipse ( brush , x , y , magnifier . Width , magnifier . Height ) ;
2015-05-08 02:32:48 +12:00
g . DrawEllipse ( Pens . White , x - 1 , y - 1 , magnifier . Width + 2 , magnifier . Height + 2 ) ;
2014-07-25 13:14:48 +12:00
g . DrawEllipse ( Pens . Black , x , y , magnifier . Width , magnifier . Height ) ;
}
2013-11-03 23:53:49 +13:00
}
}
private Bitmap Magnifier ( Image img , Point position , int horizontalPixelCount , int verticalPixelCount , int pixelSize )
{
horizontalPixelCount = ( horizontalPixelCount | 1 ) . Between ( 1 , 101 ) ;
verticalPixelCount = ( verticalPixelCount | 1 ) . Between ( 1 , 101 ) ;
pixelSize = pixelSize . Between ( 1 , 1000 ) ;
2014-07-25 13:14:48 +12:00
if ( horizontalPixelCount * pixelSize > ScreenRectangle0Based . Width | | verticalPixelCount * pixelSize > ScreenRectangle0Based . Height )
2013-11-03 23:53:49 +13:00
{
horizontalPixelCount = verticalPixelCount = 15 ;
pixelSize = 10 ;
}
int width = horizontalPixelCount * pixelSize ;
int height = verticalPixelCount * pixelSize ;
2014-07-25 13:14:48 +12:00
Bitmap bmp = new Bitmap ( width - 1 , height - 1 ) ;
2013-11-03 23:53:49 +13:00
using ( Graphics g = Graphics . FromImage ( bmp ) )
{
g . InterpolationMode = InterpolationMode . NearestNeighbor ;
g . PixelOffsetMode = PixelOffsetMode . Half ;
2014-07-25 13:14:48 +12:00
g . DrawImage ( img , new Rectangle ( 0 , 0 , width , height ) , new Rectangle ( position . X - horizontalPixelCount / 2 , position . Y - verticalPixelCount / 2 ,
horizontalPixelCount , verticalPixelCount ) , GraphicsUnit . Pixel ) ;
2013-11-03 23:53:49 +13:00
g . PixelOffsetMode = PixelOffsetMode . None ;
2014-07-25 13:14:48 +12:00
using ( SolidBrush crosshairBrush = new SolidBrush ( Color . FromArgb ( 125 , Color . LightBlue ) ) )
2013-11-03 23:53:49 +13:00
{
g . FillRectangle ( crosshairBrush , new Rectangle ( 0 , ( height - pixelSize ) / 2 , ( width - pixelSize ) / 2 , pixelSize ) ) ; // Left
g . FillRectangle ( crosshairBrush , new Rectangle ( ( width + pixelSize ) / 2 , ( height - pixelSize ) / 2 , ( width - pixelSize ) / 2 , pixelSize ) ) ; // Right
g . FillRectangle ( crosshairBrush , new Rectangle ( ( width - pixelSize ) / 2 , 0 , pixelSize , ( height - pixelSize ) / 2 ) ) ; // Top
g . FillRectangle ( crosshairBrush , new Rectangle ( ( width - pixelSize ) / 2 , ( height + pixelSize ) / 2 , pixelSize , ( height - pixelSize ) / 2 ) ) ; // Bottom
}
using ( Pen pen = new Pen ( Color . FromArgb ( 75 , Color . Black ) ) )
{
for ( int x = 1 ; x < horizontalPixelCount ; x + + )
{
2014-07-25 13:14:48 +12:00
g . DrawLine ( pen , new Point ( x * pixelSize - 1 , 0 ) , new Point ( x * pixelSize - 1 , height - 1 ) ) ;
2013-11-03 23:53:49 +13:00
}
for ( int y = 1 ; y < verticalPixelCount ; y + + )
{
2014-07-25 13:14:48 +12:00
g . DrawLine ( pen , new Point ( 0 , y * pixelSize - 1 ) , new Point ( width - 1 , y * pixelSize - 1 ) ) ;
2013-11-03 23:53:49 +13:00
}
}
2014-07-25 13:14:48 +12:00
g . DrawRectangle ( Pens . Black , ( width - pixelSize ) / 2 - 1 , ( height - pixelSize ) / 2 - 1 , pixelSize , pixelSize ) ;
g . DrawRectangle ( Pens . White , ( width - pixelSize ) / 2 , ( height - pixelSize ) / 2 , pixelSize - 2 , pixelSize - 2 ) ;
2013-11-03 23:53:49 +13:00
}
return bmp ;
}
2014-04-12 04:29:19 +12:00
private void DrawRuler ( Graphics g , Rectangle rect , Pen pen , int rulerSize , int rulerWidth )
{
if ( rect . Width > = rulerSize & & rect . Height > = rulerSize )
{
for ( int x = 1 ; x < = rect . Width / rulerWidth ; x + + )
{
g . DrawLine ( pen , new Point ( rect . X + x * rulerWidth , rect . Y ) , new Point ( rect . X + x * rulerWidth , rect . Y + rulerSize ) ) ;
g . DrawLine ( pen , new Point ( rect . X + x * rulerWidth , rect . Bottom ) , new Point ( rect . X + x * rulerWidth , rect . Bottom - rulerSize ) ) ;
}
for ( int y = 1 ; y < = rect . Height / rulerWidth ; y + + )
{
g . DrawLine ( pen , new Point ( rect . X , rect . Y + y * rulerWidth ) , new Point ( rect . X + rulerSize , rect . Y + y * rulerWidth ) ) ;
g . DrawLine ( pen , new Point ( rect . Right , rect . Y + y * rulerWidth ) , new Point ( rect . Right - rulerSize , rect . Y + y * rulerWidth ) ) ;
}
}
}
2013-11-03 23:53:49 +13:00
public void UpdateRegionPath ( )
{
regionFillPath = new GraphicsPath { FillMode = FillMode . Winding } ;
regionDrawPath = new GraphicsPath { FillMode = FillMode . Winding } ;
foreach ( Rectangle area in AreaManager . GetValidAreas )
{
AddShapePath ( regionFillPath , area ) ;
AddShapePath ( regionDrawPath , area . SizeOffset ( - 1 ) ) ;
}
}
protected virtual void AddShapePath ( GraphicsPath graphicsPath , Rectangle rect )
{
graphicsPath . AddRectangle ( rect ) ;
}
2015-01-25 13:22:46 +13:00
protected override void Dispose ( bool disposing )
{
if ( bmpSurfaceImage ! = null )
{
bmpSurfaceImage . Dispose ( ) ;
}
base . Dispose ( disposing ) ;
}
2013-11-03 23:53:49 +13:00
}
2014-10-21 08:37:51 +13:00
}