- 1 year ago
Rotating an image using Borland C++ Builder and Windows API functions
To rotate an image using Borland C++ Builder and Windows API functions, you can leverage the GDI (Graphics Device Interface) functions provided by the Windows API. Here's an example of how you can accomplish this:
cpp#include <vcl.h>
#include <jpeg.hpp>
#include <Windows.h>
#pragma hdrstop
#pragma argsused
int WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
// Load the image
TJPEGImage *image = new TJPEGImage();
image->LoadFromFile("image.jpg");
// Create a bitmap and draw the image on it
Graphics::TBitmap *bitmap = new Graphics::TBitmap();
bitmap->Assign(image);
// Create a rotated bitmap
Graphics::TBitmap *rotatedBitmap = new Graphics::TBitmap();
rotatedBitmap->Width = bitmap->Height;
rotatedBitmap->Height = bitmap->Width;
rotatedBitmap->Canvas->Draw(0, 0, bitmap->Rotate90());
// Save the rotated image to a file
rotatedBitmap->SaveToFile("rotated_image.jpg");
// Clean up
delete image;
delete bitmap;
delete rotatedBitmap;
return 0;
}
In this example, we load an image from a file using the TJPEGImage
class, create a TBitmap
object, and draw the image onto the bitmap. Then, we create another TBitmap
object called rotatedBitmap
with its width and height swapped to accommodate the rotated image.
To rotate the image, we use the Rotate90
function of the TBitmap
class, which rotates the image by 90 degrees clockwise. The rotated image is then drawn onto the rotatedBitmap
object.
Finally, we save the rotated image to a file using the SaveToFile
function of the TBitmap
class.
Note that you need to have the JPEG unit (jpeg.hpp
) included and properly set up in your project to work with JPEG images. Adjust the file paths and names according to your requirements.
This example demonstrates the basic process of rotating an image using Borland C++ Builder and Windows API functions. You can further customize and enhance the code to meet your specific needs.