/// <summary> /// i dont know why i had to do all this bs, and for some reason i can't use it without screwing everything up... /// </summary> /// <param name="filePath"></param> /// <returns></returns> private static Bitmap GetThumbnail(string filePath) { ShellFile shellFile = ShellFile.FromFilePath(filePath); BitmapSource bitmapSource = shellFile.Thumbnail.ExtraLargeBitmapSource; double newWidthRatio = 64 / (double)bitmapSource.PixelWidth; double newHeightRatio = ((64 * bitmapSource.PixelHeight) / (double)bitmapSource.PixelWidth) / (double)bitmapSource.PixelHeight; System.Windows.Media.Imaging.BitmapSource transformedBitmapSource = new System.Windows.Media.Imaging.TransformedBitmap( bitmapSource, new System.Windows.Media.ScaleTransform(newWidthRatio, newHeightRatio)); int width = transformedBitmapSource.PixelWidth; int height = transformedBitmapSource.PixelHeight; int stride = width * ((transformedBitmapSource.Format.BitsPerPixel + 7) / 8); byte[] bits = new byte[height * stride]; transformedBitmapSource.CopyPixels(bits, stride, 0); unsafe { fixed(byte *pBits = bits) { IntPtr ptr = new IntPtr(pBits); System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap( width, height, stride, System.Drawing.Imaging.PixelFormat.Format32bppArgb, ptr); bitmap = new Bitmap(filePath); return(bitmap); } } }
private void Zoom( bool zoomIn, double Scale ) { var InverseScale = 1.0 / Scale; var transformed = new TransformedBitmap( Bmp, new ScaleTransform( Scale, Scale ) ); var buffer = new byte[PixelWidth * PixelHeight * 4]; if ( zoomIn ) { var offsetX = Convert.ToInt32( PixelWidth * ( Scale - 1 ) * 0.5 ); var offsetY = Convert.ToInt32( PixelHeight * ( Scale - 1 ) * 0.5 ); transformed.CopyPixels( new Int32Rect( offsetX, offsetY, PixelWidth, PixelHeight ), buffer, PixelWidth * 4, 0 ); Bmp.WritePixels( new Int32Rect( 0, 0, PixelWidth, PixelHeight ), buffer, PixelWidth * 4, 0 ); } else { var offsetX = Convert.ToInt32( PixelWidth * ( 1 - Scale ) * 0.5 ); var offsetY = Convert.ToInt32( PixelHeight * ( 1 - Scale ) * 0.5 ); transformed.CopyPixels( new Int32Rect( 0, 0, (int)transformed.Width, (int)transformed.Height ), buffer, PixelWidth * 4, 0 ); Bmp.WritePixels( new Int32Rect( 0, 0, PixelWidth, PixelHeight ), new byte[PixelWidth * PixelHeight * 4], PixelWidth * 4, 0 ); // Clear Bmp.WritePixels( new Int32Rect( offsetX, offsetY, (int)transformed.Width, (int)transformed.Height ), buffer, PixelWidth * 4, 0 ); } RealHeight = RealHeight * InverseScale; Draw( ); }
private System.Drawing.Bitmap convertBitmapSourceToBitmap(BitmapSource paco) { //using (Stream stm = File.Open("bitmapsource_to_bitmap.jpg", FileMode.Open, FileAccess.Read)) //{ // Since we're not specifying a System.Windows.Media.Imaging.BitmapCacheOption, the pixel format // will be System.Windows.Media.PixelFormats.Pbgra32. //System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Media.Imaging.BitmapFrame.Create( // stm, // System.Windows.Media.Imaging.BitmapCreateOptions.None, // System.Windows.Media.Imaging.BitmapCacheOption.OnLoad); BitmapSource bitmapSource = paco; // Scale the image so that it will display similarly to the WPF Image. double newWidthRatio = image1.Width / (double)bitmapSource.PixelWidth; double newHeightRatio = ((image1.Width * bitmapSource.PixelHeight) / (double)bitmapSource.PixelWidth) / (double)bitmapSource.PixelHeight; System.Windows.Media.Imaging.BitmapSource transformedBitmapSource = new System.Windows.Media.Imaging.TransformedBitmap( bitmapSource, new System.Windows.Media.ScaleTransform(newWidthRatio, newHeightRatio)); int width = transformedBitmapSource.PixelWidth; int height = transformedBitmapSource.PixelHeight; int stride = width * ((transformedBitmapSource.Format.BitsPerPixel + 7) / 8); byte[] bits = new byte[height * stride]; transformedBitmapSource.CopyPixels(bits, stride, 0); unsafe { fixed (byte* pBits = bits) { IntPtr ptr = new IntPtr(pBits); System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap( width, height, stride, System.Drawing.Imaging.PixelFormat.Format32bppPArgb, ptr); return bitmap;//picture.Image = bitmap; } } //} }
private System.Drawing.Bitmap GetBitmap(System.Windows.Controls.Image image) { System.Windows.Media.Imaging.BitmapSource bitmapSource = image.Source as BitmapSource; System.Windows.Media.Imaging.BitmapSource transformedBitmapSource = new System.Windows.Media.Imaging.TransformedBitmap(bitmapSource,new System.Windows.Media.ScaleTransform(1,1)); int width = transformedBitmapSource.PixelWidth; int height = transformedBitmapSource.PixelHeight; int stride = width * ((transformedBitmapSource.Format.BitsPerPixel + 7) / 8); byte[] bits = new byte[height * stride]; transformedBitmapSource.CopyPixels(bits, stride, 0); unsafe { fixed (byte* pBits = bits) { IntPtr ptr = new IntPtr(pBits); System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap( width, height, stride, System.Drawing.Imaging.PixelFormat.Format32bppPArgb, ptr); return bitmap; } } }
public void FilterEffects() { if (ImageParameters.Instance.EffectImage != null) { try { BitmapSource b = null; int[] sourceImagePixels = null; int sourceWidth = -1; int sourceHeight = -1; Dispatcher.Invoke(DispatcherPriority.Normal, new DispatcherOperationCallback(delegate(object arg) { BitmapSource bitmap = ImageParameters.Instance.EffectImage; sourceWidth = bitmap.PixelWidth; sourceHeight = bitmap.PixelHeight; sourceImagePixels = new int[sourceWidth * sourceHeight * 4]; bitmap.CopyPixels(sourceImagePixels, sourceWidth * 4, 0); return null; } ), null); if (sourceWidth < 0 || sourceHeight < 0 || sourceImagePixels == null) { return; } //Clone so that this thread owns the image b = BitmapSource.Create(sourceWidth, sourceHeight, 96, 96, PixelFormats.Bgra32, null, sourceImagePixels, sourceWidth * 4); /* * Sequence of applying effects and operations to the image is as follows: * * Crop * Rotate * Flip * Adjustment Filters * Advanced Adjustment Filter * Effects Filters * */ if (cropData != Rect.Empty) { // Create a CroppedBitmap based off of a xaml defined resource. b = new CroppedBitmap(b, new Int32Rect( (int)(cropData.X), (int)(cropData.Y), (int)(cropData.Width), (int)(cropData.Height))); } if (ImageParameters.Instance.Rotate != Rotation.Rotate0) { TransformedBitmap rotate = new TransformedBitmap(); rotate.BeginInit(); double rotationAngle = 0.0; switch (ImageParameters.Instance.Rotate) { case Rotation.Rotate180: rotationAngle = 180.0; break; case Rotation.Rotate270: rotationAngle = 270.0; break; case Rotation.Rotate90: rotationAngle = 90.0; break; } rotate.Source = b; rotate.Transform = new RotateTransform(rotationAngle); rotate.EndInit(); int[] pixels = new int[rotate.PixelWidth * rotate.PixelHeight]; rotate.CopyPixels(pixels, rotate.PixelWidth * 4, 0); b = BitmapSource.Create(rotate.PixelWidth, rotate.PixelHeight, 96, 96, PixelFormats.Bgr32, null, pixels, rotate.PixelWidth * 4); } if (ImageParameters.Instance.Flip != FlipType.None) { TransformedBitmap flip = new TransformedBitmap(); flip.BeginInit(); double scaleX = 1.0; double scaleY = 1.0; switch (ImageParameters.Instance.Flip) { case FlipType.Horizontal: scaleX = -1.0; break; case FlipType.Vertical: scaleY = -1.0; break; case FlipType.Both: scaleX = scaleY = -1.0; break; } flip.Source = b; flip.Transform = new ScaleTransform(scaleX, scaleY); flip.EndInit(); int[] pixels = new int[flip.PixelWidth * flip.PixelHeight]; flip.CopyPixels(pixels, flip.PixelWidth * 4, 0); b = BitmapSource.Create(flip.PixelWidth, flip.PixelHeight, 96, 96, PixelFormats.Bgr32, null, pixels, flip.PixelWidth * 4); } //Run the basic adjustments b = ImageParameters.Instance.AdjustmentFilter.ExecuteFilter(b); //Do the advanced adjustments //Only filter if the filter parameters have been set if (App.DoFilter) { b = AdvancedAdjustments.Instance.Do(b); } //Apply any effects filters foreach (EffectFilter filter in ImageParameters.Instance.EffectFilters) { b = filter.ExecuteFilter(b); } //It is necessary to pass the pixels raw between threads // to avoid cross thread calls to wpf objects int[] p = new int[b.PixelWidth * b.PixelHeight]; b.CopyPixels(p, b.PixelWidth * 4, 0); int width = b.PixelWidth; int height = b.PixelHeight; PixelFormat format = b.Format; Dispatcher.BeginInvoke(DispatcherPriority.Normal, new DispatcherOperationCallback(delegate(object arg) { image2.Source = BitmapSource.Create(width, height, 96, 96, format, null, p, width * 4); return null; } ), null); } catch (Exception e) { Dispatcher.BeginInvoke(DispatcherPriority.Normal, new DispatcherOperationCallback(delegate(object arg) { image2.Source = (BitmapSource)image1.Source.Clone(); return null; } ), null); GC.Collect(); } } GC.Collect(); }