2

i have colde like this :

 Bitmap bmp = new Bitmap(width, height);

I just take a capture of window. Now i want just resize this captured bitmap (bmp). How can i cut my bmp for example

  RECT rt = new RECT();
        GetWindowRect(hwnd1, out rt);
        Int32 width = rt.Right - rt.Left;
        Int32 height = rt.Bottom - rt.Top;
        int leftttt = rt.Left + (width - 202);
        int width2 = rt.Right - leftttt;

//              // I want cut like this :
//
      //  in x=lefttt  y = rt.Top    Size ( width2,height)

And later i can easy save file to check my results by: (but won't do that only for check)

    bmp.Save(@"D:\test.jpg", ImageFormat.Jpeg);

EDIT: I Want just cut not resize . When i do code :

var graph = Graphics.FromImage(scren_kurwa.Image);

graph.DrawImage(bmp.Image, 10, 10, 200, 200);

And i save it its just override my bmp screen and just take a capture just in smaller version.

I just want to cut for examaple i want show only 1/4 of width this screen and save it to file. ( just save 1/4 width not more).

EDIT 2 :

graph.CopyFromScreen(leftttt, rt.Top, 0, 0, new Size(width2, height), CopyPixelOperation.SourceCopy);

This code above just doing what i want but i don't want again copy from screen i want copy this from bmp captured before.

Please be patient for newbies . I searched forums and just can't find solution. Thank you.

EDIT 3 I just did how you wrote :

        Rectangle cropRect = new Rectangle(100,100,100,100);
        Bitmap bmp1 = new Bitmap(bmp1.Image);
        bmp1.Clone(cropRect, bmp.PixelFormat);


        bmp1.Save(@"D:\xdddde.jpg", ImageFormat.Jpeg);

But it don't cut an image just display the same as i had bmp.

Michael
  • 202
  • 1
  • 3
  • 13
  • 1
    have you checked these questions [Scaling a System.Drawing.Bitmap to a given size while maintaining aspect ratio](http://stackoverflow.com/questions/10442269/scaling-a-system-drawing-bitmap-to-a-given-size-while-maintaining-aspect-ratio) and [Resize Image](http://stackoverflow.com/questions/10839358/resize-bitmap-image) ? – Sergey Berezovskiy Mar 28 '17 at 11:55
  • 1
    Possible duplicate of [Scaling a System.Drawing.Bitmap to a given size while maintaining aspect ratio](http://stackoverflow.com/questions/10442269/scaling-a-system-drawing-bitmap-to-a-given-size-while-maintaining-aspect-ratio) – Koby Douek Mar 28 '17 at 11:56
  • No duplicate they show how to resize only i want a new bmpfile (cutter) – Michael Mar 28 '17 at 12:30

1 Answers1

1

This should work for you:

Bitmap cuttedImage;

using(Bitmap originalImage = new Bitmap("filePathName"))
{
   Rectangle cropRect = new Rectangle(...);

   cuttedImage = originalImage .Clone(cropRect, originalBmp.PixelFormat);
}

cuttedImage.Save("filePathName", ImageFormat.Jpeg);
cuttedImage.Dispose();

Note that this will create a shallow copy of your Bitmap. In your case that does not seem to be a problem, but keep that in mind.

Also make sure to check the MSDN documentation for exception handling. Either check that the rectangle is bigger than 0 and not bigger than the original image beforehand or catch the exceptions.

Jurjen
  • 1,376
  • 12
  • 19
  • With rectangle too big I also meant 'out of bounds'. Even if your rectangle is smaller than the image, if you specify an area out of the bounds of the original image you will also get the `OutOfMemory` exception. E.g.: on a 100x100 image, if you want the rectangle placed from (45, 40) to (100,100), you need to do `new Rectangle(45, 40, 55, 90)`. Because: width = image width - x = (100 - 45 = 55), height = image height - y = (100 - 40 = 60). `new Rectangle(45, 40, 100, 100)` will give you an `OutOfMemory` exception: the rectangle is placed from (45, 40) to (145, 140) -> bigger than your image! – Jurjen Mar 28 '17 at 13:45
  • Yes man thank you i know that i just tested i now know about ourofmemory. But look my main edit just i wrote EDIT 3 – Michael Mar 28 '17 at 13:52
  • @Michael I just updated the code slightly. This should be more robust and hopefully also work for you. – Jurjen Mar 28 '17 at 14:10
  • Btw , should i just clean memoery or something with this clone? Because i try start and start project again and i have poput you dont have memoery to run project. – Michael Mar 28 '17 at 14:51
  • Yes you should. I directly updated my answer to show you how I would implement it (the code does the same but now handles disposing). The `using` statement will make sure the `originalImage` is disposed. Then I end with the `Dispose()` method on the `cuttedImage` to make sure this one is also disposed. The benefit of this method is that you can now even save to the same file location (overwrite the file) since the original resource is released. – Jurjen Mar 28 '17 at 15:04