@@ -100,6 +100,22 @@ public virtual void Clear(Color color)
100100 /// <param name="y">The Y coordinate.</param>
101101 public abstract void DrawPoint ( Color color , int x , int y ) ;
102102
103+ /// <summary>
104+ /// Sets the pixel at the given coordinates to the specified <paramref name="color"/>, without unnecessary color operations.
105+ /// </summary>
106+ /// <param name="color">The color to draw with (raw argb).</param>
107+ /// <param name="x">The X coordinate.</param>
108+ /// <param name="y">The Y coordinate.</param>
109+ public abstract void DrawPoint ( uint color , int x , int y ) ;
110+
111+ /// <summary>
112+ /// Sets the pixel at the given coordinates to the specified <paramref name="color"/>. without ToArgb()
113+ /// </summary>
114+ /// <param name="color">The color to draw with (raw argb).</param>
115+ /// <param name="x">The X coordinate.</param>
116+ /// <param name="y">The Y coordinate.</param>
117+ public abstract void DrawPoint ( int color , int x , int y ) ;
118+
103119 /// <summary>
104120 /// The name of the Canvas implementation.
105121 /// </summary>
@@ -117,11 +133,14 @@ public virtual void Clear(Color color)
117133 /// <param name="x">The X coordinate.</param>
118134 /// <param name="y">The Y coordinate.</param>
119135 public abstract Color GetPointColor ( int x , int y ) ;
136+
120137 /// <summary>
121- /// Gets the index of the pixel at the given coordinates.
138+ /// Gets the color of the pixel at the given coordinates in ARGB .
122139 /// </summary>
123140 /// <param name="x">The X coordinate.</param>
124141 /// <param name="y">The Y coordinate.</param>
142+ public abstract int GetRawPointColor ( int x , int y ) ;
143+
125144 internal int GetPointOffset ( int x , int y )
126145 {
127146 return ( x * Stride ) + ( y * Pitch ) ;
@@ -147,6 +166,47 @@ public virtual void DrawArray(Color[] colors, int x, int y, int width, int heigh
147166 }
148167 }
149168
169+ /// <summary>
170+ /// Draws an array of pixels to the canvas, starting at the given coordinates,
171+ /// using the given width.
172+ /// </summary>
173+ /// <param name="colors">The pixels to draw.</param>
174+ /// <param name="x">The X coordinate.</param>
175+ /// <param name="y">The Y coordinate.</param>
176+ /// <param name="width">The width of the drawn bitmap.</param>
177+ /// <param name="height">The height of the drawn bitmap.</param>
178+ public virtual void DrawArray ( int [ ] colors , int x , int y , int width , int height )
179+ {
180+ for ( int X = 0 ; X < width ; X ++ )
181+ {
182+ for ( int Y = 0 ; Y < height ; Y ++ )
183+ {
184+ DrawPoint ( colors [ Y * width + X ] , x + X , y + Y ) ;
185+ }
186+ }
187+ }
188+
189+ /// <summary>
190+ /// Draws an array of pixels to the canvas, starting at the given coordinates,
191+ /// using the given width.
192+ /// </summary>
193+ /// <param name="colors">The pixels to draw.</param>
194+ /// <param name="x">The X coordinate.</param>
195+ /// <param name="y">The Y coordinate.</param>
196+ /// <param name="width">The width of the drawn bitmap.</param>
197+ /// <param name="height">The height of the drawn bitmap.</param>
198+ /// <param name="startIndex">int[] colors tarting position</param>
199+ public virtual void DrawArray ( int [ ] colors , int x , int y , int width , int height , int startIndex )
200+ {
201+ for ( int X = 0 ; X < width ; X ++ )
202+ {
203+ for ( int Y = 0 ; Y < height ; Y ++ )
204+ {
205+ DrawPoint ( colors [ Y * width + X + startIndex ] , x + X , y + Y ) ;
206+ }
207+ }
208+ }
209+
150210 /// <summary>
151211 /// Draws a horizontal line.
152212 /// </summary>
@@ -317,6 +377,7 @@ public virtual void DrawCircle(Color color, int xCenter, int yCenter, int radius
317377 /// <param name="x0">The X center coordinate.</param>
318378 /// <param name="y0">The Y center coordinate.</param>
319379 /// <param name="radius">The radius of the circle to draw.</param>
380+ /// <param name="preventOffBoundPixels">Prevents drawing outside the bounds of the canvas.</param>
320381 public virtual void DrawFilledCircle ( Color color , int x0 , int y0 , int radius )
321382 {
322383 int x = radius ;
@@ -485,40 +546,17 @@ public virtual void DrawSquare(Color color, int x, int y, int size)
485546 /// <param name="height">The height of the rectangle.</param>
486547 public virtual void DrawRectangle ( Color color , int x , int y , int width , int height )
487548 {
488- /*
489- * we must draw four lines connecting any vertex of our rectangle to do this we first obtain the position of these
490- * vertex (we call these vertexes A, B, C, D as for geometric convention)
491- */
492-
493- /* The check of the validity of x and y are done in DrawLine() */
494-
495- /* The vertex A is where x,y are */
496- int xa = x ;
497- int ya = y ;
498-
499- /* The vertex B has the same y coordinate of A but x is moved of width pixels */
500- int xb = x + width ;
501- int yb = y ;
549+ // Draw top edge from (x, y) to (x + width, y)
550+ DrawLine ( color , x , y , x + width , y ) ;
502551
503- /* The vertex C has the same x coordiate of A but this time is y that is moved of height pixels */
504- int xc = x ;
505- int yc = y + height ;
552+ // Draw left edge from (x, y) to (x, y + height)
553+ DrawLine ( color , x , y , x , y + height ) ;
506554
507- /* The Vertex D has x moved of width pixels and y moved of height pixels */
508- int xd = x + width ;
509- int yd = y + height ;
555+ // Draw bottom edge from (x, y + height) to (x + width, y + height)
556+ DrawLine ( color , x , y + height , x + width , y + height ) ;
510557
511- /* Draw a line betwen A and B */
512- DrawLine ( color , xa , ya , xb , yb ) ;
513-
514- /* Draw a line between A and C */
515- DrawLine ( color , xa , ya , xc , yc ) ;
516-
517- /* Draw a line between B and D */
518- DrawLine ( color , xb , yb , xd , yd ) ;
519-
520- /* Draw a line between C and D */
521- DrawLine ( color , xc , yc , xd , yd ) ;
558+ // Draw right edge from (x + width, y) to (x + width, y + height)
559+ DrawLine ( color , x + width , y , x + width , y + height ) ;
522560 }
523561
524562 /// <summary>
@@ -569,6 +607,7 @@ public virtual void DrawTriangle(Color color, int v1x, int v1y, int v2x, int v2y
569607 /// <param name="image">The image to draw.</param>
570608 /// <param name="x">The origin X coordinate.</param>
571609 /// <param name="y">The origin Y coordinate.</param>
610+ /// <param name="preventOffBoundPixels">Prevents drawing outside the bounds of the canvas.</param>
572611 public virtual void DrawImage ( Image image , int x , int y , bool preventOffBoundPixels = true )
573612 {
574613 Color color ;
@@ -598,6 +637,35 @@ public virtual void DrawImage(Image image, int x, int y, bool preventOffBoundPix
598637 }
599638 }
600639
640+ /// <summary>
641+ /// Creates a bitmap by copying a portion of your canvas from the specified coordinates and dimensions.
642+ /// </summary>
643+ /// <param name="x">The starting X coordinate of the region to copy.</param>
644+ /// <param name="y">The starting Y coordinate of the region to copy.</param>
645+ /// <param name="width">The width of the region to copy.</param>
646+ /// <param name="height">The height of the region to copy.</param>
647+ /// <returns>A new <see cref="Bitmap"/> containing the copied region.</returns>
648+ public virtual Bitmap GetImage ( int x , int y , int width , int height )
649+ {
650+ Bitmap bitmap = new Bitmap ( ( uint ) x , ( uint ) y , ColorDepth . ColorDepth32 ) ;
651+
652+ for ( int posy = y , desty = 0 ; posy < y + y ; posy ++ , desty ++ )
653+ {
654+ for ( int posx = x , destx = 0 ; posx < x + x ; posx ++ , destx ++ )
655+ {
656+ bitmap . RawData [ desty * x + destx ] = GetRawPointColor ( posx , posy ) ;
657+ }
658+ }
659+ return bitmap ;
660+ }
661+
662+ /// <summary>
663+ /// Scales an image to the specified new width and height.
664+ /// </summary>
665+ /// <param name="image">The image to be scaled.</param>
666+ /// <param name="newWidth">The width of the scaled image.</param>
667+ /// <param name="newHeight">The height of the scaled image.</param>
668+ /// <returns>An array of integers representing the scaled image's pixel data. (Raw bitmap data)</returns>
601669 static int [ ] ScaleImage ( Image image , int newWidth , int newHeight )
602670 {
603671 int [ ] pixels = image . RawData ;
@@ -607,7 +675,6 @@ static int[] ScaleImage(Image image, int newWidth, int newHeight)
607675 int xRatio = ( int ) ( ( w1 << 16 ) / newWidth ) + 1 ;
608676 int yRatio = ( int ) ( ( h1 << 16 ) / newHeight ) + 1 ;
609677 int x2 , y2 ;
610-
611678 for ( int i = 0 ; i < newHeight ; i ++ )
612679 {
613680 for ( int j = 0 ; j < newWidth ; j ++ )
@@ -617,7 +684,6 @@ static int[] ScaleImage(Image image, int newWidth, int newHeight)
617684 temp [ ( i * newWidth ) + j ] = pixels [ ( y2 * w1 ) + x2 ] ;
618685 }
619686 }
620-
621687 return temp ;
622688 }
623689
@@ -629,6 +695,7 @@ static int[] ScaleImage(Image image, int newWidth, int newHeight)
629695 /// <param name="y">The Y coordinate.</param>
630696 /// <param name="w">The desired width to scale the image to before drawing.</param>
631697 /// <param name="h">The desired height to scale the image to before drawing</param>
698+ /// <param name="preventOffBoundPixels">Prevents drawing outside the bounds of the canvas.</param>
632699 public virtual void DrawImage ( Image image , int x , int y , int w , int h , bool preventOffBoundPixels = true )
633700 {
634701 Color color ;
@@ -660,12 +727,39 @@ public virtual void DrawImage(Image image, int x, int y, int w, int h, bool prev
660727 }
661728 }
662729
730+ /// <summary>
731+ /// Draws the given image at the specified coordinates, cropping the image to fit within the maximum width and height.
732+ /// </summary>
733+ /// <param name="image">The image to draw.</param>
734+ /// <param name="x">The X coordinate where the image will be drawn.</param>
735+ /// <param name="y">The Y coordinate where the image will be drawn.</param>
736+ /// <param name="maxWidth">The maximum width to display the image. If the image exceeds this width, it will be cropped.</param>
737+ /// <param name="maxHeight">The maximum height to display the image. If the image exceeds this height, it will be cropped.</param>
738+ /// <param name="preventOffBoundPixels">Prevents drawing outside the bounds of the canvas.</param>
739+ public virtual void CroppedDrawImage ( Image image , int x , int y , int maxWidth , int maxHeight , bool preventOffBoundPixels = true )
740+ {
741+ Color color ;
742+ int width = Math . Min ( ( int ) image . Width , maxWidth ) ;
743+ int height = Math . Min ( ( int ) image . Height , maxHeight ) ;
744+ int [ ] pixels = image . RawData ;
745+
746+ for ( int xi = 0 ; xi < width ; xi ++ )
747+ {
748+ for ( int yi = 0 ; yi < height ; yi ++ )
749+ {
750+ color = Color . FromArgb ( pixels [ xi + ( yi * image . Width ) ] ) ;
751+ DrawPoint ( color , x + xi , y + yi ) ;
752+ }
753+ }
754+ }
755+
663756 /// <summary>
664757 /// Draws an image with alpha blending.
665758 /// </summary>
666759 /// <param name="image">The image to draw.</param>
667760 /// <param name="x">The X coordinate.</param>
668761 /// <param name="y">The Y coordinate.</param>
762+ /// <param name="preventOffBoundPixels">Prevents drawing outside the bounds of the canvas.</param>
669763 public void DrawImageAlpha ( Image image , int x , int y , bool preventOffBoundPixels = true )
670764 {
671765 Color color ;
0 commit comments