C# rotate bitmap 90 degrees
What about this:
private void RotateAndSaveImage(String input, String output)
{
//create an object that we can use to examine an image file
using (Image img = Image.FromFile(input))
{
//rotate the picture by 90 degrees and re-save the picture as a Jpeg
img.RotateFlip(RotateFlipType.Rotate90FlipNone);
img.Save(output, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
Rotating a bitmap 90 degrees
v = (v & 0x000000000f0f0f0fUL) << 004 | (v & 0x00000000f0f0f0f0UL) << 040 |
(v & 0xf0f0f0f000000000UL) >> 004 | (v & 0x0f0f0f0f00000000UL) >> 040;
v = (v & 0x0000333300003333UL) << 002 | (v & 0x0000cccc0000ccccUL) << 020 |
(v & 0xcccc0000cccc0000UL) >> 002 | (v & 0x3333000033330000UL) >> 020;
v = (v & 0x0055005500550055UL) << 001 | (v & 0x00aa00aa00aa00aaUL) << 010 |
(v & 0xaa00aa00aa00aa00UL) >> 001 | (v & 0x5500550055005500UL) >> 010;
Rotate a saved bitmap in android
void rotate(float x)
{
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.tedd);
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
int newWidth = 200;
int newHeight = 200;
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
matrix.postRotate(x);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,width, height, matrix, true);
iv.setScaleType(ScaleType.CENTER);
iv.setImageBitmap(resizedBitmap);
}
How do you rotate a bitmap an arbitrary number of degrees?
I did some searching for you and found this:
public static Bitmap RotateImage(Bitmap b, float angle)
{
//create a new empty bitmap to hold rotated image
Bitmap returnBitmap = new Bitmap(b.Width, b.Height);
//make a graphics object from the empty bitmap
using(Graphics g = Graphics.FromImage(returnBitmap))
{
//move rotation point to center of image
g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2);
//rotate
g.RotateTransform(angle);
//move image back
g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2);
//draw passed in image onto graphics object
g.DrawImage(b, new Point(0, 0));
}
return returnBitmap;
}
Rotating a bitmap 90 degrees and saving
I found the answer!
For JPG files, apparently you can just niftily edit the exif data using the following method:
ExifInterface exif = new ExifInterface(myFile.getAbsolutePath());
exif.setAttribute(ExifInterface.TAG_ORIENTATION, "6");
exif.saveAttributes();
Are there any downsides to using this approach? It seems to work pretty well as far as I can determine..
How to Rotate Image 90 degrees
You'd want to use a Gdk.Pixbuf, which uses the GDK+ libraries to manipulate images.
var img = Gdk.Pixbuf.from_file(input);
var rotate_image = img.rotate_simple(90);
rotate_image.save(output, "jpeg");
It's worth noting that Vala is not meant to be directly compatible with C#.
Related Topics
Android Studio 3.0 Manifest Error: Unknown Element <Action> Found
Android: Gles20: Called Unimplemented Opengl Es API
Error: Failed to Resolve: Com.Android.Support:Appcompat-V7:29.0.1'
Navigation Drawer with Backword Compatibility Android
Android Save Checkbox State in Listview with Cursor Adapter
Save Data and Change Orientation
How to Read Contacts in Android Using Realm
How to Decompile an APK or Dex File on Android Platform
How to Change Color of Android Listview Separator Line
Mediarecorder Start Failed: -38
Android Devices with Different Height Takes Same Layout Folder
Stuck with Getting Camera Pic When Using the Tab Activity
Value of a Global Variable Is Reset After It Is Initialised in Valueeventlistener
Clear Recent Apps Wipe the Apps Memory and My Receiver Stopped Working
Run Lint When Building Android Studio Projects