/// <summary> /// Converts byte array of an image to BitmapSource reflecting color profiles. /// </summary> /// <param name="sourceData">Byte array of source image</param> /// <param name="colorProfilePath">Color profile file path used by a monitor</param> /// <returns>BitmapSource</returns> public static async Task <BitmapSource> ConvertImageAsync(byte[] sourceData, string colorProfilePath) { using (var ms = new MemoryStream()) { await ms.WriteAsync(sourceData, 0, sourceData.Length).ConfigureAwait(false); ms.Seek(0, SeekOrigin.Begin); var frame = BitmapFrame.Create( ms, BitmapCreateOptions.IgnoreColorProfile | BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); var ccb = new ColorConvertedBitmap(); ccb.BeginInit(); ccb.Source = frame; ccb.SourceColorContext = (frame.ColorContexts?.Any() == true) ? frame.ColorContexts.First() : new ColorContext(PixelFormats.Bgra32); // Fallback color profile ccb.DestinationColorContext = !string.IsNullOrEmpty(colorProfilePath) ? new ColorContext(new Uri(colorProfilePath)) : new ColorContext(PixelFormats.Bgra32); // Fallback color profile ccb.DestinationFormat = PixelFormats.Bgra32; ccb.EndInit(); ccb.Freeze(); return(ccb); } }
public BitmapSource ColorConvertedRead(string path, ColorProfileType from) { BitmapSource r = null; var bmi = new BitmapImage(); bmi.BeginInit(); bmi.UriSource = new Uri(path, UriKind.RelativeOrAbsolute); bmi.CacheOption = BitmapCacheOption.OnLoad; bmi.EndInit(); bmi.Freeze(); if (0 == ColorProfileName(from).CompareTo(MonitorProfileName)) { // color space is the same. r = bmi; } else { var ccb = new ColorConvertedBitmap(); ccb.BeginInit(); ccb.Source = bmi; ccb.SourceColorContext = mColorCtx[(int)from]; ccb.DestinationColorContext = mColorCtx[(int)ColorProfileType.Monitor]; ccb.EndInit(); ccb.Freeze(); r = ccb; } IsVideo = false; return(r); }
/// <summary> /// Converts color profile of BitmapSource for color management. /// </summary> /// <param name="bitmapSource">Source BitmapSource</param> /// <param name="sourceProfile">Source color profile</param> /// <param name="destinationProfile">Destination color profile</param> /// <returns>Outcome BitmapSource</returns> /// <remarks> /// Source color profile is color profile embedded in image file and destination color profile is /// color profile used by the monitor to which the Window belongs. /// </remarks> public static BitmapSource ConvertColorProfile(BitmapSource bitmapSource, ColorContext sourceProfile, ColorContext destinationProfile) { var bitmapConverted = new ColorConvertedBitmap(); bitmapConverted.BeginInit(); bitmapConverted.Source = bitmapSource; bitmapConverted.SourceColorContext = sourceProfile; bitmapConverted.DestinationColorContext = destinationProfile; bitmapConverted.DestinationFormat = PixelFormats.Bgra32; bitmapConverted.EndInit(); return(bitmapConverted); }
public int VReadImage(long posToSeek, ColorProfileType from, out BitmapSource bs, ref long duration, ref long timeStamp) { int dpi = 96; var pf = PixelFormats.Bgr32; int hr = videoRead.ReadImage(posToSeek, out WWMFVideoReader.VideoImage vi); if (hr < 0) { bs = BitmapSource.Create(0, 0, dpi, dpi, pf, null, null, 1); return(hr); } var bytesPerPixel = (pf.BitsPerPixel + 7) / 8; var stride = bytesPerPixel * vi.w; var wb = new WriteableBitmap(vi.w, vi.h, dpi, dpi, pf, null); wb.Lock(); wb.WritePixels(new Int32Rect(0, 0, vi.w, vi.h), vi.img, stride, 0); wb.Unlock(); wb.Freeze(); if (0 == ColorProfileName(from).CompareTo(MonitorProfileName)) { // color space is the same. bs = wb; } else { var ccb = new ColorConvertedBitmap(); ccb.BeginInit(); ccb.Source = wb; ccb.SourceColorContext = mColorCtx[(int)from]; ccb.DestinationColorContext = mColorCtx[(int)ColorProfileType.Monitor]; ccb.EndInit(); ccb.Freeze(); bs = ccb; } duration = vi.duration; timeStamp = vi.timeStamp; IsVideo = true; return(hr); }
private void CreateAndShowMainWindow() { // Create the application's main window mainWindow = new Window(); mainWindow.Title = "BitmapImage Sample"; //<Snippet1> // Define a BitmapImage. Image myImage = new Image(); BitmapImage bi = new BitmapImage(); // Begin initialization. bi.BeginInit(); // Set properties. bi.CacheOption = BitmapCacheOption.OnDemand; bi.CreateOptions = BitmapCreateOptions.DelayCreation; bi.DecodePixelHeight = 125; bi.DecodePixelWidth = 125; bi.Rotation = Rotation.Rotate90; MessageBox.Show(bi.IsDownloading.ToString()); bi.UriSource = new Uri("smiley.png", UriKind.Relative); // End initialization. bi.EndInit(); myImage.Source = bi; myImage.Stretch = Stretch.None; myImage.Margin = new Thickness(5); //</Snippet1> //Define a Second BitmapImage and Source Image myImage2 = new Image(); BitmapImage bi2 = new BitmapImage(); bi2.BeginInit(); bi2.DecodePixelHeight = 75; bi2.DecodePixelWidth = 75; bi2.CacheOption = BitmapCacheOption.None; bi2.CreateOptions = BitmapCreateOptions.PreservePixelFormat; bi2.Rotation = Rotation.Rotate180; bi2.UriSource = new Uri("smiley.png", UriKind.Relative); bi2.EndInit(); myImage2.Source = bi2; myImage2.Stretch = Stretch.None; myImage2.Margin = new Thickness(5); //<Snippet2> Stream imageStream = new FileStream("tulipfarm.jpg", FileMode.Open, FileAccess.Read, FileShare.Read); BitmapSource myBitmapSource = BitmapFrame.Create(imageStream); BitmapFrame myBitmapSourceFrame = (BitmapFrame)myBitmapSource; ColorContext sourceColorContext = myBitmapSourceFrame.ColorContexts[0]; ColorContext destColorContext = new ColorContext(PixelFormats.Bgra32); ColorConvertedBitmap ccb = new ColorConvertedBitmap(myBitmapSource, sourceColorContext, destColorContext, PixelFormats.Pbgra32); Image myImage3 = new Image(); myImage3.Source = ccb; myImage3.Stretch = Stretch.None; imageStream.Close(); //</Snippet2> //Show ColorConvertedBitmap Properties Stream imageStream2 = new FileStream("tulipfarm.jpg", FileMode.Open, FileAccess.Read, FileShare.Read); BitmapSource myBitmapSource2 = BitmapFrame.Create(imageStream2); BitmapFrame myBitmapSourceFrame2 = (BitmapFrame)myBitmapSource; //<Snippet3> ColorConvertedBitmap myColorConvertedBitmap = new ColorConvertedBitmap(); myColorConvertedBitmap.BeginInit(); myColorConvertedBitmap.SourceColorContext = myBitmapSourceFrame2.ColorContexts[0]; myColorConvertedBitmap.Source = myBitmapSource2; myColorConvertedBitmap.DestinationFormat = PixelFormats.Pbgra32; myColorConvertedBitmap.DestinationColorContext = new ColorContext(PixelFormats.Bgra32); myColorConvertedBitmap.EndInit(); //</Snippet3> //Define a StackPanel StackPanel myStackPanel = new StackPanel(); // Add the images to the parent StackPanel myStackPanel.Children.Add(myImage); myStackPanel.Children.Add(myImage2); myStackPanel.Children.Add(myImage3); // Add the StackPanel as the Content of the Parent Window Object mainWindow.Content = myStackPanel; mainWindow.Show(); }