WPF – converting Bitmap to ImageSource

For others, this works: //If you get ‘dllimport unknown’-, then add ‘using System.Runtime.InteropServices;’ [DllImport(“gdi32.dll”, EntryPoint = “DeleteObject”)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool DeleteObject([In] IntPtr hObject); public ImageSource ImageSourceFromBitmap(Bitmap bmp) { var handle = bmp.GetHbitmap(); try { return Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } finally { DeleteObject(handle); } }

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); } }

Image Orientation – Android

You need to account for all orientations not just 90 or 180. I am using this File curFile = new File(“path-to-file”); // … This is an image file from my device. Bitmap rotatedBitmap; try { ExifInterface exif = new ExifInterface(curFile.getPath()); int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); int rotationInDegrees = exifToDegrees(rotation); Matrix matrix = new Matrix(); if … Read more

Captured Photo orientation is changing in android

I had the same problem mostly with the Samsung handsets.Apparently Samsung phones set the EXIF orientation tag, rather than rotating individual pixels.Reading the Bitmap using BitmapFactory does not support this tag.What i found the solution to this problem was using ExifInterface in onActivityResult method of the activity.Which checks for orientation associated with URI of the … Read more

how to fill color in image in particular area?

I found the Solution with Flood fill algoritham private void FloodFill(Bitmap bmp, Point pt, int targetColor, int replacementColor){ Queue<Point> q = new LinkedList<Point>(); q.add(pt); while (q.size() > 0) { Point n = q.poll(); if (bmp.getPixel(n.x, n.y) != targetColor) continue; Point w = n, e = new Point(n.x + 1, n.y); while ((w.x > 0) && … Read more

Getting a DrawingContext for a wpf WriteableBitmap

I found sixlettervariables’ solution the most workable one. However, there’s a “drawingContext.Close()” missing. According to MSDN, “A DrawingContext must be closed before its content can be rendered”. The result is the following utility function: public static BitmapSource CreateBitmap( int width, int height, double dpi, Action<DrawingContext> render) { DrawingVisual drawingVisual = new DrawingVisual(); using (DrawingContext drawingContext … Read more

Cannot find Bitmap Class in Class Library (.NET Standard)

I’m the author of CoreCompat.System.Drawing. If you’re on .NET Core 2.0, I’d recommend you’d move to System.Drawing.Common instead, which is the Microsoft-maintained implementation of System.Drawing for .NET Core. If you’re on Linux or macOS, make sure to install libgdiplus. On macOS, run brew install mono-libgdiplus; on Linux your package manager should provide you with a … Read more