public System.Windows.Media.Imaging.WriteableBitmap ToBitmap() { var bmp = new System.Windows.Media.Imaging.WriteableBitmap(Width, Height); bmp.SetSource(new MemoryStream(Pixels)); bmp.Invalidate(); return bmp; }
public static int[] GetPixels(System.Windows.Media.Imaging.BitmapImage imageSource) { System.Windows.Media.Imaging.WriteableBitmap wbmp = new System.Windows.Media.Imaging.WriteableBitmap(imageSource); int[] pixels = new int[wbmp.PixelWidth * wbmp.PixelHeight]; wbmp.CopyPixels(pixels, wbmp.PixelWidth * 4, 0); return pixels; }
private void CreateDragVisual() { ClearDragVisual(); var bitmap = new System.Windows.Media.Imaging.WriteableBitmap(this, null); bitmap.Render(this, null); bitmap.Invalidate(); _dragVisual = new System.Windows.Controls.Image(); _dragVisual.Source = bitmap; // find topmost canvas.. (so we can add the drag visual as a child // and ensure that it is on top of everything) // var canvas = this.GetTopmostParentOfType<Canvas>(); canvas.Children.Add(_dragVisual); var point = this.GetRelativePosition(canvas); _dragVisual.SetValue(Canvas.TopProperty, point.Y); _dragVisual.SetValue(Canvas.LeftProperty, point.X); // Really make sure the drag visual is on top // Canvas.SetZIndex(_dragVisual, Int16.MaxValue); }
public static byte[] EncodeRawToJpeg(ref int[][] rawImage) { try { var bitmap = new System.Windows.Media.Imaging.WriteableBitmap(rawImage.Length, rawImage[0].Length); int width = rawImage.Length; int height = rawImage[0].Length; for (int col = 0; col < width; col++) { for (int row = 0; row < height; row++) { bitmap.Pixels[(row * width) + col] = rawImage[col][row]; } } using (MemoryStream memoryStream = new MemoryStream()) { System.Windows.Media.Imaging.Extensions.SaveJpeg(bitmap, memoryStream, width, height, 0, 100); memoryStream.Seek(0, SeekOrigin.Begin); bitmap = null; rawImage = null; GC.Collect(); return memoryStream.ToArray(); } } catch (Exception) { //- Notice! //On Server or Desktop plataform, call the reference function by using reflection //Equals made in Android plataform, because this libaries aren't portable between WP and .NET 4.0! Type bitmapType = Type.GetType("Ufc.MiscNet.ImageUtilities, MiscNet, Version=1.0.0.0, Culture=neutral"); object instance = Activator.CreateInstance(bitmapType); Type[] typeParam = new Type[1] { typeof(int[][]) }; MethodInfo method = bitmapType.GetMethod("EncodeRawToJpeg", typeParam); return (byte[]) method.Invoke(instance, new object[1] { rawImage }); } }
public static int[][] DecodeJpegToRaw(ref byte[] image) { int[][] rawImage = null; try { using (MemoryStream stream = new MemoryStream(image)) { var bitmapImage = new System.Windows.Media.Imaging.BitmapImage(); bitmapImage.SetSource(stream); var bitmap = new System.Windows.Media.Imaging.WriteableBitmap(bitmapImage); int width = bitmap.PixelWidth; int height = bitmap.PixelHeight; rawImage = new int[width][]; for (int col = 0; col < width; col++) { rawImage[col] = new int[height]; for (int row = 0; row < height; row++) { rawImage[col][row] = bitmap.Pixels[(row * width) + col]; } } bitmap = null; bitmapImage = null; GC.Collect(); } } catch (Exception) { //- Notice! //On Server or Desktop plataform, call the reference function by using reflection //Equals made in Android plataform, because this libaries aren't portable between WP and .NET 4.0! Type bitmapType = Type.GetType("Ufc.MiscNet.ImageUtilities, MiscNet, Version=1.0.0.0, Culture=neutral"); object instance = Activator.CreateInstance(bitmapType); Type[] typeParam = new Type[1] { typeof(byte[]) }; MethodInfo method = bitmapType.GetMethod("DecodeJpegToRaw", typeParam); rawImage = (int[][])method.Invoke(instance, new object[1] { image }); } finally { image = null; GC.Collect(); } return rawImage; }
public override void UpdateColorPlaneBitmap(System.Windows.Media.Imaging.WriteableBitmap bitmap, int normalComponentValue) { unsafe { bitmap.Lock(); byte * pStart = (byte *)(void *)bitmap.BackBuffer; int currentPixel = -1; double iRowUnit = (double)1; double iColUnit = (double)1; double iRowCurrent = 127; double l = (double)normalComponentValue; for (int iRow = 0; iRow < bitmap.PixelHeight; iRow++) { double b = iRowCurrent; double iColCurrent = -128; for (int iCol = 0; iCol < bitmap.PixelWidth; iCol++) { double theta = 6.0 / 29.0; double a = iColCurrent; double fy = (l + 16) / 116.0; double fx = fy + (a / 500.0); double fz = fy - (b / 200.0); var x = (fx > theta) ? D65X * (fx * fx * fx) : (fx - 16.0 / 116.0) * 3 * (theta * theta) * D65X; var y = (fy > theta) ? D65Y * (fy * fy * fy) : (fy - 16.0 / 116.0) * 3 * (theta * theta) * D65Y; var z = (fz > theta) ? D65Z * (fz * fz * fz) : (fz - 16.0 / 116.0) * 3 * (theta * theta) * D65Z; x = (x > 0.9505) ? 0.9505 : ((x < 0) ? 0 : x); y = (y > 1.0) ? 1.0 : ((y < 0) ? 0 : y); z = (z > 1.089) ? 1.089 : ((z < 0) ? 0 : z); double[] Clinear = new double[3]; Clinear[0] = x * 3.2410 - y * 1.5374 - z * 0.4986; // red Clinear[1] = -x * 0.9692 + y * 1.8760 - z * 0.0416; // green Clinear[2] = x * 0.0556 - y * 0.2040 + z * 1.0570; // blue for (int i = 0; i < 3; i++) { Clinear[i] = (Clinear[i] <= 0.0031308) ? 12.92 * Clinear[i] : (1 + 0.055) * Math.Pow(Clinear[i], (1.0 / 2.4)) - 0.055; Clinear[i] = Math.Min(Clinear[i], 1); Clinear[i] = Math.Max(Clinear[i], 0); } currentPixel++; *(pStart + currentPixel * 3 + 0) = Convert.ToByte(Clinear[2] * 255); //Blue *(pStart + currentPixel * 3 + 1) = Convert.ToByte(Clinear[1] * 255); //Green *(pStart + currentPixel * 3 + 2) = Convert.ToByte(Clinear[0] * 255); //red iColCurrent += iColUnit; } iRowCurrent -= iRowUnit; } bitmap.AddDirtyRect(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight)); bitmap.Unlock(); } }
private void LayoutRoot_Loaded(object sender, RoutedEventArgs e) { string str = ""; if (NavigationContext.QueryString.TryGetValue("tile", out str)) { Rectangle rect = new Rectangle(); rect.Width = 62; rect.Height = 62; rect.Fill = new SolidColorBrush((Color)Application.Current.Resources["PhoneAccentColor"]); using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { using (var stream = new IsolatedStorageFileStream(str + ".folder", FileMode.OpenOrCreate, store)) { using (var reader = new StreamReader(stream)) { curMenu = new menu(); try { var serial = new XmlSerializer(typeof(menu)); curMenu = (menu)serial.Deserialize(stream); } catch { } } } int y = 0; foreach (appentry app in curMenu.apps) { StackPanel panel = new StackPanel(); panel.Orientation = System.Windows.Controls.Orientation.Horizontal; System.Windows.Media.Imaging.WriteableBitmap wb = new System.Windows.Media.Imaging.WriteableBitmap(62, 62); wb.Render(rect, null); Image img = new Image(); img.Margin = new Thickness(1, 1, 10, 1); img.Width = 62; img.Height = 62; System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage(); using (var file = store.OpenFile(app.guid + ".jpg", FileMode.Open, FileAccess.Read)) { bmp.SetSource(file); } img.Source = bmp; wb.Render(img, null); wb.Invalidate(); img.Source = wb; img.VerticalAlignment = System.Windows.VerticalAlignment.Center; panel.Children.Add(img); TextBlock text = new TextBlock(); text.Text = app.name; text.FontSize = 31; text.FontWeight = FontWeights.Light; text.VerticalAlignment = System.Windows.VerticalAlignment.Center; panel.Children.Add(text); panel.Name = "panel_" + y; panel.Margin = new Thickness(1, 1, 1, 10); ListBoxItem foo = new ListBoxItem(); foo.Content = panel; foo.Tap += new EventHandler <GestureEventArgs>(panel_Tap); foo.Name = "danel_" + y; stackPanel1.Children.Add(foo); y += 1; wb = null; } PageTitle.Text = curMenu.name; PageTitle.Opacity = op; scrollViewer1.Opacity = op; scrollViewer1.Margin = new Thickness(46, top, 0, 0); } } else { //Not a folder. Go to the editor NavigationService.Navigate(new Uri("/MainPage.xaml?init=true", UriKind.Relative)); } tim = new System.Windows.Threading.DispatcherTimer(); tim.Interval = new TimeSpan(0, 0, 0, 0, 10); tim.Tick += new EventHandler(tim_Tick); tim.Start(); }
/// <summary> /// Convert HObject to BitmapImage /// </summary> /// <param name="HImg"></param> /// <returns></returns> public static System.Windows.Media.Imaging.BitmapImage Hobject2Bitmapimage(HalconDotNet.HObject HImg) { if (HImg == null) { return(null); } try { System.Windows.Media.Imaging.BitmapImage bmpimg = new System.Windows.Media.Imaging.BitmapImage(); HalconDotNet.HTuple Channels; HalconDotNet.HOperatorSet.CountChannels(HImg, out Channels); if (Channels.I == 1) { System.Windows.Media.PixelFormat pixelFmt = System.Windows.Media.PixelFormats.Gray8; HalconDotNet.HTuple hpointer, type, width, height; HalconDotNet.HOperatorSet.GetImagePointer1(HImg, out hpointer, out type, out width, out height); System.Windows.Media.Imaging.WriteableBitmap MetaImg = new System.Windows.Media.Imaging.WriteableBitmap(width.I, height.I, 96, 96, pixelFmt, null); MetaImg.Lock(); unsafe { byte *data = (byte *)MetaImg.BackBuffer; byte *img_ptr = (byte *)hpointer.IP; for (int i = 0; i < width.I * height.I; i++) { *(data + i) = (*(img_ptr + i)); } } MetaImg.Unlock(); bmpimg = ConvertWriteablebitmapToBitmapimage(MetaImg); } else if (Channels.I == 3) { System.Windows.Media.PixelFormat pixelFmt = System.Windows.Media.PixelFormats.Bgr24; HalconDotNet.HTuple hred, hgreen, hblue, type, width, height; HalconDotNet.HOperatorSet.GetImagePointer3(HImg, out hred, out hgreen, out hblue, out type, out width, out height); System.Windows.Media.Imaging.WriteableBitmap MetaImg = new System.Windows.Media.Imaging.WriteableBitmap(width.I, height.I, 96, 96, pixelFmt, null); MetaImg.Lock(); unsafe { byte *data = (byte *)MetaImg.BackBuffer; byte *hr = (byte *)hred.IP; byte *hg = (byte *)hgreen.IP; byte *hb = (byte *)hblue.IP; for (int i = 0; i < width.I * height.I; i++) { *(data + (i * 3)) = (*(hb + i)); *(data + (i * 3) + 1) = *(hg + i); *(data + (i * 3) + 2) = *(hr + i); } } MetaImg.Unlock(); bmpimg = ConvertWriteablebitmapToBitmapimage(MetaImg); } else if (Channels.I == 4) { System.Windows.Media.PixelFormat pixelFmt = System.Windows.Media.PixelFormats.Pbgra32; HalconDotNet.HTuple hred, hgreen, hblue, type, width, height; HalconDotNet.HOperatorSet.GetImagePointer3(HImg, out hred, out hgreen, out hblue, out type, out width, out height); System.Windows.Media.Imaging.WriteableBitmap MetaImg = new System.Windows.Media.Imaging.WriteableBitmap(width.I, height.I, 96, 96, pixelFmt, null); MetaImg.Lock(); unsafe { byte *data = (byte *)MetaImg.BackBuffer; byte *hr = (byte *)hred.IP; byte *hg = (byte *)hgreen.IP; byte *hb = (byte *)hblue.IP; for (int i = 0; i < width.I * height.I; i++) { *(data + (i * 4)) = *(hb + i); *(data + (i * 4) + 1) = *(hg + i); *(data + (i * 4) + 2) = *(hr + i); *(data + (i * 4) + 3) = 255; } } MetaImg.Unlock(); bmpimg = ConvertWriteablebitmapToBitmapimage(MetaImg); } else { bmpimg = null; } return(bmpimg); } catch (HalconDotNet.HalconException ex) { Console.WriteLine("In ImageTypeConverter.Hobject2Bitmapimage: " + ex.Message); return(null); } catch (System.Exception ex) { Console.WriteLine("In ImageTypeConverter.Hobject2Bitmapimage: " + ex.Message); return(null); } }
/// <summary> /// Convert WriteableBitmap to BitmapImage. /// refer to https://stackoverflow.com/questions/14161665/how-do-i-convert-a-writeablebitmap-object-to-a-bitmapimage-object-in-wpf /// </summary> /// <param name="wbm"></param> /// <returns></returns> public static System.Windows.Media.Imaging.BitmapImage ConvertWriteablebitmapToBitmapimage(System.Windows.Media.Imaging.WriteableBitmap wbm) { System.Windows.Media.Imaging.BitmapImage bmpimg = new System.Windows.Media.Imaging.BitmapImage(); using (System.IO.MemoryStream stream = new System.IO.MemoryStream()) { System.Windows.Media.Imaging.PngBitmapEncoder encoder = new System.Windows.Media.Imaging.PngBitmapEncoder(); encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(wbm)); encoder.Save(stream); bmpimg.BeginInit(); bmpimg.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; bmpimg.StreamSource = stream; bmpimg.EndInit(); bmpimg.Freeze(); } return(bmpimg); }
/// <summary>Decodes the given image file and returns an uncompressed bitmap.</summary> /// <param name="filePath">The name and path of the image file to be decoded.</param> /// <returns> /// <para>Returns the result of the image decoding operation.</para> /// <para> /// If the result object's HasSucceeded property is set true, then this decoder succeeded in decoding /// the given image file and the result object's Bitmap property would provide the bitmap that was produced. /// </para> /// <para> /// If the result object's HasSucceeded property is false, then this decoder failed to decode the /// given image file. The reason why would then be provided by the result object's Message property. /// </para> /// </returns> public WinRT.Interop.Graphics.BitmapResult DecodeFromFile(string filePath) { // Validate the given image file path. if (String.IsNullOrEmpty(filePath)) { return(WinRT.Interop.Graphics.BitmapResult.FailedWith("Image decoder given a null or empty string.")); } // Check that the given file exists and if its an external or embedded resource file. bool isExternalFile = true; var resourceServices = Corona.WinRT.Interop.Storage.DotNetResourceServices.Instance; if (System.IO.File.Exists(filePath) == false) { if (resourceServices.ContainsFile(filePath) == false) { String message = String.Format("Image file '{0}' not found.", filePath); return(WinRT.Interop.Graphics.BitmapResult.FailedWith(message)); } isExternalFile = false; } // Ensure that the preferred pixel format is in the supported pixel format set. fSettings.SupportedPixelFormats.Add(fSettings.PreferredPixelFormat); Corona.WinRT.Interop.Graphics.IBitmap bitmap = null; System.IO.Stream stream = null; try { if (isExternalFile) { stream = System.IO.File.OpenRead(filePath); } else { stream = resourceServices.OpenFile(filePath); } var writeableBitmap = new System.Windows.Media.Imaging.WriteableBitmap(0, 0); writeableBitmap.SetSource(stream); bitmap = DotNetBitmap.From(writeableBitmap); writeableBitmap = null; // Convert the loaded bitmap if it does not meet the decoder's requirements. if ((fSettings.SupportedPixelFormats.Contains(bitmap.Info.PixelFormat) == false) || (bitmap.Info.PixelWidth > fSettings.MaxPixelWidth) || (bitmap.Info.PixelHeight > fSettings.MaxPixelHeight)) { var converter = new WinRT.Interop.Graphics.BitmapConverter(); converter.CopySettingsFrom(this); // if (converter.CanConvert(bitmap)) // { // converter.Convert(bitmap); // } // else // { var convertedBitmap = converter.CreateBitmapFrom(bitmap); bitmap.ReleaseByteBuffer(); bitmap = convertedBitmap; // } } } catch (Exception ex) { if (bitmap != null) { try { bitmap.ReleaseByteBuffer(); } catch (Exception) { } } return(WinRT.Interop.Graphics.BitmapResult.FailedWith(ex.Message)); } finally { if (stream != null) { try { stream.Close(); stream.Dispose(); } catch (Exception) { } } } if (bitmap == null) { return(WinRT.Interop.Graphics.BitmapResult.FailedWith("Failed to load bitmap")); } return(WinRT.Interop.Graphics.BitmapResult.SucceededWith(bitmap)); }
public WPFBitmapWrapper(int bitmapWidth, int bitmapHeight, double xdpi, double ydpi) { bitmap = new System.Windows.Media.Imaging.WriteableBitmap(bitmapWidth, bitmapHeight, xdpi, ydpi, pixelFormat, null); }
public static void SetPixels(WIC_WRITABLE dstBmp, int dstX, int dstY, SpanBitmap srcSpan) { // https://docs.microsoft.com/en-us/dotnet/api/system.windows.media.imaging.writeablebitmap.adddirtyrect?view=netframework-4.8 srcSpan.PinReadablePointer(ptr => SetPixels(dstBmp, dstX, dstY, ptr)); }
private void LayoutRoot_Loaded(object sender, RoutedEventArgs e) { NavigationContext.QueryString.TryGetValue("folder", out folderName); if (folderName != "") { PageTitle.Text = "edit: " + folderName; using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { using (var stream = new IsolatedStorageFileStream(folderName + ".folder", FileMode.OpenOrCreate, store)) { using (var reader = new StreamReader(stream)) { curMenu = new menu(); try { var serial = new XmlSerializer(typeof(menu)); curMenu = (menu)serial.Deserialize(stream); } catch { } } } } if (curMenu.apps == null) { curMenu.apps = new System.Collections.ObjectModel.Collection <appentry>(); } } else { MessageBox.Show("I didn't get any arguments...", "Well this is awkward", MessageBoxButton.OK); } Rectangle rect = new Rectangle(); rect.Width = 62; rect.Height = 62; rect.Fill = new SolidColorBrush((Color)Application.Current.Resources["PhoneAccentColor"]); using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { int y = 0; foreach (appentry app in clsData.installedApps) { try { System.Windows.Media.Imaging.WriteableBitmap wb = new System.Windows.Media.Imaging.WriteableBitmap(52, 52); StackPanel panel = new StackPanel(); panel.Orientation = System.Windows.Controls.Orientation.Horizontal; wb.Render(rect, null); Image img = new Image(); img.Margin = new Thickness(1, 1, 10, 1); img.Width = 52; img.Height = 52; System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage(); using (var file = store.OpenFile(app.guid + ".jpg", FileMode.Open, FileAccess.Read)) { bmp.SetSource(file); } System.Diagnostics.Debug.WriteLine("apps/" + app.guid + ".jpg"); //bmp.SetSource(Application.GetResourceStream(new Uri("apps/" + app.guid + ".jpg", UriKind.Relative)).Stream); //System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage(new Uri("/" + app.image, UriKind.Relative)); img.Source = bmp; wb.Render(img, null); wb.Invalidate(); img.Source = wb; img.VerticalAlignment = System.Windows.VerticalAlignment.Center; TextBlock text = new TextBlock(); text.Text = app.name; text.FontSize = 30; text.FontWeight = FontWeights.Light; text.VerticalAlignment = System.Windows.VerticalAlignment.Center; CheckBox cb = new CheckBox(); cb.Name = "app_" + app.guid; foreach (appentry folderapp in curMenu.apps) { if (folderapp.guid == app.guid) { cb.IsChecked = true; } } cb.Checked += new RoutedEventHandler(cb_Checked); cb.Unchecked += new RoutedEventHandler(cb_Unchecked); selectedApps.Add(cb); //panel.Name = app.guid; panel.Name = "panel_" + y; //panel.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(panel_Tap); panel.Margin = new Thickness(0, 1, 1, 10); panel.Children.Add(cb); panel.Children.Add(img); panel.Children.Add(text); stackPanel1.Children.Add(panel); y += 1; //wb = null; } catch (Exception ez) { //MessageBox.Show(ez.Message + " " + ez.StackTrace); } } } }
public static void SetPixels(this WIC_WRITABLE bmp, int dstX, int dstY, SpanBitmap spanSrc) { _Implementation.SetPixels(bmp, dstX, dstY, spanSrc); }
public static MemoryBitmap.IDisposableSource UsingMemoryBitmap(this WIC_WRITABLE src) { return(new WPFMemoryManager(src)); }
public override unsafe System.Windows.Media.Imaging.BitmapSource ToBitmapSource() { System.Windows.Media.Imaging.WriteableBitmap bitmap; if (HasTransparency()) { bitmap = new System.Windows.Media.Imaging.WriteableBitmap( width, height, dpiX, dpiY, System.Windows.Media.PixelFormats.Bgra32, null); bitmap.Lock(); try { byte *destScan0 = (byte *)bitmap.BackBuffer; int destStride = bitmap.BackBufferStride; for (int y = 0; y < height; y++) { ColorBgra8 *src = (ColorBgra8 *)GetRowAddressUnchecked(y); byte * dst = destScan0 + (y * destStride); for (int x = 0; x < width; x++) { dst[0] = src->B; dst[1] = src->G; dst[2] = src->R; dst[3] = src->A; src++; dst += 4; } } } finally { bitmap.Unlock(); } } else { bitmap = new System.Windows.Media.Imaging.WriteableBitmap( width, height, dpiX, dpiY, System.Windows.Media.PixelFormats.Bgr24, null); bitmap.Lock(); try { byte *destScan0 = (byte *)bitmap.BackBuffer; int destStride = bitmap.BackBufferStride; for (int y = 0; y < height; y++) { ColorBgra8 *src = (ColorBgra8 *)GetRowAddressUnchecked(y); byte * dst = destScan0 + (y * destStride); for (int x = 0; x < width; x++) { dst[0] = src->B; dst[1] = src->G; dst[2] = src->R; src++; dst += 3; } } } finally { bitmap.Unlock(); } } bitmap.Freeze(); return(bitmap); }