本文整理汇总了C#中System.Windows.Media.Imaging.WriteableBitmap.Freeze方法的典型用法代码示例。如果您正苦于以下问题:C# WriteableBitmap.Freeze方法的具体用法?C# WriteableBitmap.Freeze怎么用?C# WriteableBitmap.Freeze使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Media.Imaging.WriteableBitmap
的用法示例。
在下文中一共展示了WriteableBitmap.Freeze方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ToBitmapSource
/// <summary>
/// To the bitmap source.
/// </summary>
/// <param name="bitmap">The bitmap.</param>
/// <param name="format">The format.</param>
/// <param name="creationOptions">The creation options.</param>
/// <param name="cacheOptions">The cache options.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">bitmap</exception>
public static BitmapSource ToBitmapSource(this Bitmap bitmap, ImageFormat format, BitmapCreateOptions creationOptions = BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption cacheOptions = BitmapCacheOption.OnLoad)
{
if (bitmap == null)
throw new ArgumentNullException("bitmap");
using (MemoryStream memoryStream = new MemoryStream())
{
try
{
// You need to specify the image format to fill the stream.
// I'm assuming it is PNG
bitmap.Save(memoryStream, format);
memoryStream.Seek(0, SeekOrigin.Begin);
BitmapDecoder bitmapDecoder = BitmapDecoder.Create(
memoryStream,
creationOptions,
cacheOptions);
// This will disconnect the stream from the image completely...
WriteableBitmap writable =
new WriteableBitmap(bitmapDecoder.Frames.Single());
writable.Freeze();
return writable;
}
catch (Exception)
{
return null;
}
}
}
示例2: ThreadSafeImage
public static BitmapSource ThreadSafeImage(Stream stream)
{
var result = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
result.Freeze();
var writable = new WriteableBitmap(result);
writable.Freeze();
return writable;
}
示例3: LoadImageFromExtraImage
public BitmapSource LoadImageFromExtraImage(ExtraImage item, BitmapSource extraImage)
{
var image = new WriteableBitmap(item.Width, item.Height, 96, 96, PixelFormats.Pbgra32, null);
var rect = new Int32Rect(item.X, item.Y, item.Width, item.Height);
image.Lock();
extraImage.CopyPixels(rect, image.BackBuffer, image.BackBufferStride * image.PixelHeight, image.BackBufferStride);
image.Unlock();
image.Freeze();
return image;
}
示例4: AsWriteable
public static WriteableBitmap AsWriteable(this BitmapSource bitmapSource)
{
if (bitmapSource is WriteableBitmap)
{
return bitmapSource as WriteableBitmap;
}
WriteableBitmap writeableBitmap = new WriteableBitmap(bitmapSource);
writeableBitmap.Freeze();
return writeableBitmap;
}
示例5: GetBitmapSourceFromBitmap
protected BitmapSource GetBitmapSourceFromBitmap(Bitmap bitmap, ImageFormat imageFormat)
{
using (MemoryStream memoryStream = new MemoryStream())
{
bitmap.Save(memoryStream, imageFormat);
BitmapDecoder bitmapDecoder = BitmapDecoder.Create(memoryStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
// This will disconnect the stream from the image completely...
WriteableBitmap writable = new WriteableBitmap(bitmapDecoder.Frames.Single());
writable.Freeze();
return writable;
}
}
示例6: CreateBitmapSourceFromBitmap
private static BitmapSource CreateBitmapSourceFromBitmap(Stream stream)
{
BitmapDecoder bitmapDecoder = BitmapDecoder.Create(
stream,
BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.OnLoad);
// This will disconnect the stream from the image completely...
WriteableBitmap writable = new WriteableBitmap(bitmapDecoder.Frames.Single());
writable.Freeze();
return writable;
}
示例7: CopyBitmap
public static void CopyBitmap(BitmapSource source, WriteableBitmap target, bool dispatcher, int spacing, bool freezeBitmap)
{
var width = source.PixelWidth;
var height = source.PixelHeight;
var stride = width * ((source.Format.BitsPerPixel + 7) / 8);
var bits = new byte[height * stride];
source.CopyPixels(bits, stride, 0);
if (dispatcher)
{
target.Dispatcher.BeginInvoke(DispatcherPriority.Background,
new ThreadStart(delegate
{
//UI Thread
var delta = target.Height - height;
var newWidth = width > target.Width ? (int)target.Width : width;
var outRect = new Int32Rect((int)((target.Width - newWidth) / 2), (int)(delta >= 0 ? delta : 0) / 2 + spacing, newWidth - (spacing * 2), newWidth - (spacing * 2));
try
{
target.WritePixels(outRect, bits, stride, 0);
if (freezeBitmap)
{
target.Freeze();
}
}
catch (Exception e)
{
Debug.WriteLine(e);
Debugger.Break();
}
}));
}
else
{
var delta = target.Height - height;
var newWidth = width > target.Width ? (int)target.Width : width;
var outRect = new Int32Rect(spacing, (int)(delta >= 0 ? delta : 0) / 2 + spacing, newWidth - (spacing * 2), newWidth - (spacing * 2));
try
{
target.WritePixels(outRect, bits, stride, 0);
if (freezeBitmap)
target.Freeze();
}
catch (Exception e)
{
Debug.WriteLine(e);
Debugger.Break();
}
}
}
示例8: encodeAndSubmitUserPhotoToCMS
public void encodeAndSubmitUserPhotoToCMS(WriteableBitmap _imageBMP)
{
createBGWorker();
// The currently rendered BMP sits on the main thread but its a
// copy already anyway.
_workingImageCopy = _imageBMP;
_workingImageCopy.Freeze();
if (_imageSubmissionBGWorker.IsBusy != true)
{
// Start the asynchronous operation.
_imageSubmissionBGWorker.RunWorkerAsync();
}
}
开发者ID:guozanhua,项目名称:MFDetroit2013_Kinect_GreenScreen_PhotoKiosk,代码行数:15,代码来源:ThreadedPhotoSubmission.cs
示例9: CreateBackgroundSource
public static ImageSource CreateBackgroundSource(SongBackground bg, int width = -1)
{
if (bg.Type == SongBackgroundType.Image)
{
try
{
Uri uri;
if (width > -1 && width <= 300)
uri = DataManager.Backgrounds.GetFile(bg).PreviewUri;
else
uri = DataManager.Backgrounds.GetFile(bg).Uri;
var img = new BitmapImage();
img.BeginInit();
img.UriSource = uri;
if (width > 300)
img.DecodePixelWidth = width;
img.EndInit();
// Without an additional frozen WritableBitmap loading locally is delayed (hangs)
// For remote URIs this doesn't work however, so we're not using it then
if (uri.IsFile)
{
WriteableBitmap writable = new WriteableBitmap(img);
writable.Freeze();
return writable;
}
else
{
return img;
}
}
catch
{
return CreateColorImage(Brushes.Black);
}
}
else if (bg.Type == SongBackgroundType.Color)
{
var c = bg.Color;
var brush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(c.A, c.R, c.G, c.B));
return CreateColorImage(brush);
}
else
{
return CreateColorImage(Brushes.Black); // TODO: load video preview?
}
}
示例10: Convert
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (DesignerProperties.GetIsInDesignMode(new DependencyObject())) return null;
using (var stream = new FileStream((string)value, FileMode.Open))
{
var decoder = BitmapDecoder.Create(
stream,
BitmapCreateOptions.None,
BitmapCacheOption.OnLoad);
var bitmap = new WriteableBitmap(decoder.Frames[0]);
bitmap.Freeze();
return bitmap;
}
}
示例11: FromFile
public static Texture2D FromFile(Device device, string path)
{
using (Stream stream = new FileStream(
path,
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite | FileShare.Delete
))
{
var decoder = BitmapDecoder.Create(
stream,
BitmapCreateOptions.None,
BitmapCacheOption.Default
);
var bmp = new WriteableBitmap(decoder.Frames[0]);
bmp.Freeze();
return FromBitmap(device, bmp);
}
}
示例12: ConvertImage
public static BitmapSource ConvertImage(Bitmap bitmap)
{
if (bitmap == null)
throw new ArgumentNullException("bitmap");
using (MemoryStream bitmapStream = new MemoryStream())
{
bitmap.Save(bitmapStream, ImageFormat.Png);
bitmapStream.Seek(0, SeekOrigin.Begin);
BitmapDecoder decoder = BitmapDecoder.Create
(
bitmapStream,
BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.OnLoad
);
var output = new WriteableBitmap(decoder.Frames.Single());
output.Freeze();
return output;
}
}
示例13: GetMetadata
public void GetMetadata(BitmapFile file, WriteableBitmap writeableBitmap)
{
Exiv2Helper exiv2Helper = new Exiv2Helper();
try
{
exiv2Helper.Load(file.FileItem.FileName, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight);
file.Metadata.Clear();
foreach (var exiv2Data in exiv2Helper.Tags)
{
file.Metadata.Add(new DictionaryItem() {Name = exiv2Data.Value.Tag, Value = exiv2Data.Value.Value});
}
if (ServiceProvider.Settings.ShowFocusPoints)
{
writeableBitmap.Lock();
foreach (Rect focuspoint in exiv2Helper.Focuspoints)
{
DrawRect(writeableBitmap, (int) focuspoint.X, (int) focuspoint.Y,
(int) (focuspoint.X + focuspoint.Width),
(int) (focuspoint.Y + focuspoint.Height), Colors.Aqua,
ServiceProvider.Settings.LowMemoryUsage ? 2 : 7);
}
writeableBitmap.Unlock();
}
if (exiv2Helper.Tags.ContainsKey("Exif.Image.Orientation") && !file.FileItem.IsRaw)
{
if (exiv2Helper.Tags["Exif.Image.Orientation"].Value == "bottom, right")
writeableBitmap = writeableBitmap.Rotate(180);
if (exiv2Helper.Tags["Exif.Image.Orientation"].Value == "right, top")
writeableBitmap = writeableBitmap.Rotate(90);
if (exiv2Helper.Tags["Exif.Image.Orientation"].Value == "left, bottom")
writeableBitmap = writeableBitmap.Rotate(270);
}
if (ServiceProvider.Settings.RotateIndex != 0)
{
switch (ServiceProvider.Settings.RotateIndex)
{
case 1:
writeableBitmap = writeableBitmap.Rotate(90);
break;
case 2:
writeableBitmap = writeableBitmap.Rotate(180);
break;
case 3:
writeableBitmap = writeableBitmap.Rotate(270);
break;
}
}
}
catch (Exception exception)
{
Log.Error("Error loading metadata ", exception);
}
writeableBitmap.Freeze();
file.DisplayImage = writeableBitmap;
file.InfoLabel = Path.GetFileName(file.FileItem.FileName);
file.InfoLabel += String.Format(" | {0}x{1}", exiv2Helper.Width, exiv2Helper.Height);
if (exiv2Helper.Tags.ContainsKey("Exif.Photo.ExposureTime"))
file.InfoLabel += " | E " + exiv2Helper.Tags["Exif.Photo.ExposureTime"].Value;
if (exiv2Helper.Tags.ContainsKey("Exif.Photo.FNumber"))
file.InfoLabel += " | " + exiv2Helper.Tags["Exif.Photo.FNumber"].Value;
if (exiv2Helper.Tags.ContainsKey("Exif.Photo.ISOSpeedRatings"))
file.InfoLabel += " | ISO " + exiv2Helper.Tags["Exif.Photo.ISOSpeedRatings"].Value;
if (exiv2Helper.Tags.ContainsKey("Exif.Photo.ExposureBiasValue"))
file.InfoLabel += " | " + exiv2Helper.Tags["Exif.Photo.ExposureBiasValue"].Value;
if (exiv2Helper.Tags.ContainsKey("Exif.Photo.FocalLength"))
file.InfoLabel += " | " + exiv2Helper.Tags["Exif.Photo.FocalLength"].Value;
}
示例14: GetBitmap
public void GetBitmap(BitmapFile bitmapFile)
{
if (_isworking)
{
_nextfile = bitmapFile;
return;
}
_nextfile = null;
_isworking = true;
_currentfile = bitmapFile;
_currentfile.RawCodecNeeded = false;
if (!File.Exists(_currentfile.FileItem.FileName))
{
Log.Error("File not found " + _currentfile.FileItem.FileName);
StaticHelper.Instance.SystemMessage = "File not found " + _currentfile.FileItem.FileName;
}
else
{
//Metadata.Clear();
try
{
if (_currentfile.FileItem.IsRaw)
{
WriteableBitmap writeableBitmap = null;
Log.Debug("Loading raw file.");
BitmapDecoder bmpDec = BitmapDecoder.Create(new Uri(_currentfile.FileItem.FileName),
BitmapCreateOptions.None,
BitmapCacheOption.Default);
if (bmpDec.CodecInfo != null)
Log.Debug("Raw codec: " + bmpDec.CodecInfo.FriendlyName);
if (bmpDec.Thumbnail != null)
{
WriteableBitmap bitmap = new WriteableBitmap(bmpDec.Thumbnail);
bitmap.Freeze();
bitmapFile.DisplayImage = bitmap;
}
if (ServiceProvider.Settings.LowMemoryUsage)
{
if (bmpDec.Thumbnail != null)
{
writeableBitmap = BitmapFactory.ConvertToPbgra32Format(bmpDec.Thumbnail);
}
else
{
writeableBitmap = BitmapFactory.ConvertToPbgra32Format(bmpDec.Frames[0]);
double dw = 2000/writeableBitmap.Width;
writeableBitmap = writeableBitmap.Resize((int) (writeableBitmap.PixelWidth*dw),
(int) (writeableBitmap.PixelHeight*dw),
WriteableBitmapExtensions.Interpolation.
Bilinear);
}
bmpDec = null;
}
else
{
writeableBitmap = BitmapFactory.ConvertToPbgra32Format(bmpDec.Frames.Single());
}
GetMetadata(_currentfile, writeableBitmap);
Log.Debug("Loading raw file done.");
}
else
{
BitmapImage bi = new BitmapImage();
// BitmapImage.UriSource must be in a BeginInit/EndInit block.
bi.BeginInit();
if (ServiceProvider.Settings.LowMemoryUsage)
bi.DecodePixelWidth = 2000;
bi.UriSource = new Uri(_currentfile.FileItem.FileName);
bi.EndInit();
WriteableBitmap writeableBitmap = BitmapFactory.ConvertToPbgra32Format(bi);
GetMetadata(_currentfile, writeableBitmap);
Log.Debug("Loading bitmap file done.");
}
}
catch (FileFormatException)
{
_currentfile.RawCodecNeeded = true;
Log.Debug("Raw codec not installed or unknown file format");
StaticHelper.Instance.SystemMessage = "Raw codec not installed or unknown file format";
}
catch (Exception exception)
{
Log.Error(exception);
}
if (_nextfile == null)
{
Thread threadPhoto = new Thread(GetAdditionalData);
threadPhoto.Start(_currentfile);
_currentfile.OnBitmapLoaded();
_currentfile = null;
_isworking = false;
}
else
{
_isworking = false;
GetBitmap(_nextfile);
}
//.........这里部分代码省略.........
示例15: DrawGrid
private void DrawGrid(WriteableBitmap writeableBitmap)
{
System.Windows.Media.Color color = Colors.White;
color.A = 50;
if (OverlayActivated)
{
if ((SelectedOverlay != null && File.Exists(SelectedOverlay)) || OverlayUseLastCaptured)
{
if (OverlayUseLastCaptured)
{
if (File.Exists(ServiceProvider.Settings.SelectedBitmap.FileItem.LargeThumb) &&
_lastOverlay != ServiceProvider.Settings.SelectedBitmap.FileItem.LargeThumb)
{
_lastOverlay = ServiceProvider.Settings.SelectedBitmap.FileItem.LargeThumb;
_overlayImage = null;
}
}
if (_overlayImage == null)
{
BitmapImage bitmapSource = new BitmapImage();
bitmapSource.DecodePixelWidth = writeableBitmap.PixelWidth;
bitmapSource.BeginInit();
bitmapSource.UriSource = new Uri(OverlayUseLastCaptured ? _lastOverlay : SelectedOverlay);
bitmapSource.EndInit();
_overlayImage = BitmapFactory.ConvertToPbgra32Format(bitmapSource);
_overlayImage.Freeze();
}
int x = writeableBitmap.PixelWidth*OverlayScale/100;
int y = writeableBitmap.PixelHeight*OverlayScale/100;
int xx = writeableBitmap.PixelWidth*OverlayHorizontal/100;
int yy = writeableBitmap.PixelWidth*OverlayVertical/100;
System.Windows.Media.Color transpColor = Colors.White;
//set color transparency for blit only the alpha chanel is used from transpColor
if (OverlayTransparency < 100)
transpColor = System.Windows.Media.Color.FromArgb((byte) (0xff*OverlayTransparency/100d), 0xff, 0xff, 0xff);
writeableBitmap.Blit(
new Rect(0 + (x / 2) + xx, 0 + (y / 2) + yy, writeableBitmap.PixelWidth - x,
writeableBitmap.PixelHeight - y),
_overlayImage,
new Rect(0, 0, _overlayImage.PixelWidth, _overlayImage.PixelHeight), transpColor,
WriteableBitmapExtensions.BlendMode.Alpha);
}
}
switch (GridType)
{
case 1:
{
for (int i = 1; i < 3; i++)
{
writeableBitmap.DrawLine(0, (int)((writeableBitmap.Height / 3) * i),
(int)writeableBitmap.Width,
(int)((writeableBitmap.Height / 3) * i), color);
writeableBitmap.DrawLine((int)((writeableBitmap.Width / 3) * i), 0,
(int)((writeableBitmap.Width / 3) * i),
(int)writeableBitmap.Height, color);
}
writeableBitmap.SetPixel((int)(writeableBitmap.Width / 2), (int)(writeableBitmap.Height / 2), 128,
Colors.Red);
}
break;
case 2:
{
for (int i = 1; i < 10; i++)
{
writeableBitmap.DrawLine(0, (int)((writeableBitmap.Height / 10) * i),
(int)writeableBitmap.Width,
(int)((writeableBitmap.Height / 10) * i), color);
writeableBitmap.DrawLine((int)((writeableBitmap.Width / 10) * i), 0,
(int)((writeableBitmap.Width / 10) * i),
(int)writeableBitmap.Height, color);
}
writeableBitmap.SetPixel((int)(writeableBitmap.Width / 2), (int)(writeableBitmap.Height / 2), 128,
Colors.Red);
}
break;
case 3:
{
writeableBitmap.DrawLineDDA(0, 0, (int)writeableBitmap.Width,
(int)writeableBitmap.Height, color);
writeableBitmap.DrawLineDDA(0, (int)writeableBitmap.Height,
(int)writeableBitmap.Width, 0, color);
writeableBitmap.SetPixel((int)(writeableBitmap.Width / 2), (int)(writeableBitmap.Height / 2), 128,
Colors.Red);
}
break;
case 4:
{
writeableBitmap.DrawLineDDA(0, (int)(writeableBitmap.Height / 2), (int)writeableBitmap.Width,
(int)(writeableBitmap.Height / 2), color);
writeableBitmap.DrawLineDDA((int)(writeableBitmap.Width / 2), 0,
(int)(writeableBitmap.Width / 2), (int)writeableBitmap.Height, color);
writeableBitmap.SetPixel((int)(writeableBitmap.Width / 2), (int)(writeableBitmap.Height / 2), 128,
Colors.Red);
}
//.........这里部分代码省略.........