/// <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); }
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); }
/// <summary> /// Converts a System.Drawing.Image to a BitmapImage which is compatible /// with WPF /// </summary> /// <param name="image">The image.</param> /// <returns></returns> //internal static BitmapSource GetColourCorrectedImageSource(DrawingImage image) internal static BitmapSource GetBitmapImage2(DrawingImage image) { var memoryStream = new MemoryStream(); image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png); var frame = BitmapFrame.Create(memoryStream); // Windows ICC profile location: C:\Windows\System32\spool\drivers\color // Adobe ICC profiles: C:\Program Files (x86)\Common Files\Adobe\Color var iccProfilePath = @"C:\Program Files (x86)\Common Files\Adobe\Color\MPProfiles\FilmTheaterK2395PD.icc"; Uri destinationProfileUri = new Uri(iccProfilePath); ColorContext scc = new ColorContext(destinationProfileUri); // Using frame.Format is the same as loading the sRGB default ColorContext dcc = new ColorContext(frame.Format); //ColorContext scc = new ColorContext(new Uri(@"C:\Windows\System32\spool\drivers\color\sRGB Color Space Profile.icm")); // NOTE: the source and destination colorcontexes seem conceptually reversed. var colorConvertedBitmap = new ColorConvertedBitmap((BitmapSource)frame, scc, dcc, frame.Format); colorConvertedBitmap.Freeze(); return(colorConvertedBitmap); }