本文整理汇总了C#中System.Windows.Media.Imaging.TransformedBitmap类的典型用法代码示例。如果您正苦于以下问题:C# TransformedBitmap类的具体用法?C# TransformedBitmap怎么用?C# TransformedBitmap使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TransformedBitmap类属于System.Windows.Media.Imaging命名空间,在下文中一共展示了TransformedBitmap类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ApplyOrientation
internal static BitmapSource ApplyOrientation(BitmapSource bitmap, BitmapMetadata metadata)
{
if (metadata == null || !metadata.ContainsQuery("System.Photo.Orientation"))
return bitmap;
ushort orientation = (ushort)metadata.GetQuery("System.Photo.Orientation");
switch (orientation)
{
case 2: // flip horizontal
return new TransformedBitmap(bitmap, new ScaleTransform(-1, 1));
case 3: // rotate 180
return new TransformedBitmap(bitmap, new RotateTransform(-180));
case 4: // flip vertical
return new TransformedBitmap(bitmap, new ScaleTransform(1, -1));
case 5: // transpose
bitmap = new TransformedBitmap(bitmap, new ScaleTransform(1, -1));
goto case 8;
case 6: // rotate 270
return new TransformedBitmap(bitmap, new RotateTransform(-270));
case 7: // transverse
bitmap = new TransformedBitmap(bitmap, new ScaleTransform(-1, 1));
goto case 8;
case 8: // rotate 90
return new TransformedBitmap(bitmap, new RotateTransform(-90));
default:
return bitmap;
}
}
示例2: DrawImage
public static void DrawImage (this ConsoleBuffer @this, ImageSource imageSource, int x, int y, int width, int height)
{
var bmp = imageSource as BitmapSource;
if (bmp == null)
throw new ArgumentException("Only rendering of bitmap source is supported.");
@this.OffsetX(ref x).OffsetY(ref y);
int x1 = x, x2 = x + width, y1 = y, y2 = y + height;
if ([email protected](ref x1, ref y1, ref x2, ref y2))
return;
if (width != bmp.PixelWidth || height != bmp.PixelHeight)
bmp = new TransformedBitmap(bmp, new ScaleTransform((double)width / bmp.PixelWidth, (double)height / bmp.PixelHeight));
if (bmp.Format != PixelFormats.Indexed4)
bmp = new FormatConvertedBitmap(bmp, PixelFormats.Indexed4, BitmapPalettes.Halftone8Transparent, 0.5);
const int bitsPerPixel = 4;
int stride = 4 * (bmp.PixelWidth * bitsPerPixel + 31) / 32;
byte[] bytes = new byte[stride * bmp.PixelHeight];
bmp.CopyPixels(bytes, stride, 0);
for (int iy = y1, py = 0; iy < y2; iy++, py++) {
ConsoleChar[] charsLine = @this.GetLine(iy);
for (int ix = x1, px = 0; ix < x2; ix++, px++) {
int byteIndex = stride * py + px / 2;
int bitOffset = px % 2 == 0 ? 4 : 0;
SetColor(ref charsLine[ix], bmp.Palette, (bytes[byteIndex] >> bitOffset) & 0xF);
}
}
}
示例3: ResizeAsync
/// <summary>
/// Resizes the image presented by the <paramref name="imageData"/> to a <paramref name="newSize"/>.
/// </summary>
/// <param name="imageData">
/// The binary data of the image to resize.
/// </param>
/// <param name="newSize">
/// The size to which to resize the image.
/// </param>
/// <param name="keepAspectRatio">
/// A flag indicating whether to save original aspect ratio.
/// </param>
/// <returns>
/// The structure which contains binary data of resized image and the actial size of resized image depending on <paramref name="keepAspectRatio"/> value.
/// </returns>
/// <exception cref="InvalidOperationException">
/// An error occurred during resizing the image.
/// </exception>
public static Task<ImageInfo> ResizeAsync(this byte[] imageData, Size newSize, bool keepAspectRatio)
{
var result = new ImageInfo();
var image = imageData.ToBitmap();
var percentWidth = (double) newSize.Width/(double) image.PixelWidth;
var percentHeight = (double) newSize.Height/(double) image.PixelHeight;
ScaleTransform transform;
if (keepAspectRatio)
{
transform = percentWidth < percentHeight
? new ScaleTransform {ScaleX = percentWidth, ScaleY = percentWidth}
: new ScaleTransform {ScaleX = percentHeight, ScaleY = percentHeight};
}
else
{
transform = new ScaleTransform {ScaleX = percentWidth, ScaleY = percentHeight};
}
var resizedImage = new TransformedBitmap(image, transform);
using (var memoryStream = new MemoryStream())
{
var jpegEncoder = new JpegBitmapEncoder();
jpegEncoder.Frames.Add(BitmapFrame.Create(resizedImage));
jpegEncoder.Save(memoryStream);
result.Data = memoryStream.ToArray();
result.Size = new Size(resizedImage.PixelWidth, resizedImage.PixelHeight);
}
return Task.FromResult(result);
}
示例4: TryGetBitmapTransform
public bool TryGetBitmapTransform(string optionValue, IEnumerable<KeyValuePair<string, string>> settings, out Func<BitmapSource, BitmapSource> bitmapTransformerFunc)
{
var cropOriginFunc = GetCropPointFunc(GetSetting(settings, "cropOrigin", "center,center"));
var cropSize = GetCropSize(optionValue);
bitmapTransformerFunc = bitmapSource =>
{
double widthScale = cropSize.Width / bitmapSource.PixelWidth;
double heightScale = cropSize.Height / bitmapSource.PixelHeight;
double scale = Math.Max(widthScale, heightScale);
bitmapSource = new TransformedBitmap(bitmapSource, new ScaleTransform(scale, scale));
var cropOrigin = cropOriginFunc(bitmapSource);
var x = cropOrigin.X - (cropSize.Width / 2);
x = Math.Max(0, x);
x = Math.Min(x, bitmapSource.PixelWidth - cropSize.Width);
var y = cropOrigin.Y - (cropSize.Height / 2);
y = Math.Max(0, y);
y = Math.Min(y, bitmapSource.PixelHeight - cropSize.Height);
return new CroppedBitmap(bitmapSource, new Int32Rect((int)Math.Round(x), (int)Math.Round(y), (int)cropSize.Width, (int)cropSize.Height));
};
return true;
}
示例5: Shrink
public void Shrink(string sourcePath)
{
Debug.Assert(!String.IsNullOrWhiteSpace(sourcePath));
BitmapDecoder decoder;
BitmapEncoder encoder;
GetBitmapDecoderEncoder(sourcePath, out decoder, out encoder);
if (decoder == null || encoder == null)
throw new ArgumentException("Image type is not supported.");
// NOTE: grab its first (and usually only) frame. Only TIFF and GIF images support multiple frames
var sourceFrame = decoder.Frames[0];
// Apply the transform
var transform = GetTransform(sourceFrame);
if (transform == null)
return;
var transformedBitmap = new TransformedBitmap(sourceFrame, transform);
// Create the destination frame
var thumbnail = sourceFrame.Thumbnail;
var metadata = sourceFrame.Metadata as BitmapMetadata;
var colorContexts = sourceFrame.ColorContexts;
var destinationFrame = BitmapFrame.Create(transformedBitmap, thumbnail, metadata, colorContexts);
encoder.Frames.Add(destinationFrame);
SaveResultToFile(sourcePath, encoder);
}
示例6: PageLoaded
public void PageLoaded(object sender, RoutedEventArgs args)
{
// Create Image element.
Image rotated90 = new Image();
rotated90.Width = 150;
// Create the TransformedBitmap to use as the Image source.
TransformedBitmap tb = new TransformedBitmap();
// Create the source to use as the tb source.
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(@"sampleImages/watermelon.jpg", UriKind.RelativeOrAbsolute);
bi.EndInit();
// Properties must be set between BeginInit and EndInit calls.
tb.BeginInit();
tb.Source = bi;
// Set image rotation.
RotateTransform transform = new RotateTransform(90);
tb.Transform = transform;
tb.EndInit();
// Set the Image source.
rotated90.Source = tb;
//Add Image to the UI
Grid.SetColumn(rotated90, 1);
Grid.SetRow(rotated90, 1);
transformedGrid.Children.Add(rotated90);
}
示例7: GetTransformedBitmapFromUri
private TransformedBitmap GetTransformedBitmapFromUri(string uri)
{
var bitmap = new Bitmap(uri);
var requiredRotation = GetRotationFromImage(bitmap);
var bitmapImage = new BitmapImage(new Uri(uri));
var transform = new RotateTransform(requiredRotation);
var tb = new TransformedBitmap(bitmapImage, transform);
return tb;
}
示例8: ResizeImage
private static byte[] ResizeImage(ImageFormat format, BitmapSource photo, double? width, double? height)
{
var scaleX = (width.GetValueOrDefault(photo.Width) / photo.Width);
var scaleY = (height.GetValueOrDefault(photo.Height) / photo.Height);
var target = new TransformedBitmap(photo, new ScaleTransform(scaleX, scaleY, 0, 0));
var targetFrame = BitmapFrame.Create(target);
return targetFrame.ToByteArray(format);
}
示例9: buttonRotate_Click
private void buttonRotate_Click(object sender, RoutedEventArgs e)
{
TransformedBitmap tb = new TransformedBitmap();
tb.BeginInit();
tb.Transform = new RotateTransform(90);
tb.Source = this.imageViewModel.LoadedBitmap;
tb.EndInit();
this.imageViewModel.LoadedBitmap = tb;
this.imageViewModel.ClearPositions();
}
示例10: TransformSave
public static void TransformSave(BitmapSource bi, double scale, int quality, string filename)
{
var tr = new ScaleTransform(scale, scale);
TransformedBitmap tb = new TransformedBitmap(bi, tr);
//if (File.Exists(filename)) File.Delete(filename);
var stream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
JpegBitmapEncoder encoder = new System.Windows.Media.Imaging.JpegBitmapEncoder();
encoder.QualityLevel = quality;
encoder.Frames.Add(BitmapFrame.Create(tb));
encoder.Save(stream);
stream.Close();
}
示例11: ShowMyFace
public ShowMyFace()
{
Title = "Show My Face";
///******************************************************************
// 3���� ShowMyFace ����
//******************************************************************/
Uri uri = new Uri("http://www.charlespetzold.com/PetzoldTattoo.jpg");
//BitmapImage bitmap = new BitmapImage(uri);
//Image img = new Image();
//img.Source = bitmap;
//Content = img;
///******************************************************************
// p1245 BitmapImage �ڵ�
//******************************************************************/
Image rotated90 = new Image();
TransformedBitmap tb = new TransformedBitmap();
FormatConvertedBitmap fb = new FormatConvertedBitmap();
CroppedBitmap cb = new CroppedBitmap();
// Create the source to use as the tb source.
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = uri;
bi.EndInit();
//cb.BeginInit();
//cb.Source = bi;
//Int32Rect rect = new Int32Rect();
//rect.X = 220;
//rect.Y = 200;
//rect.Width = 120;
//rect.Height = 80;
//cb.SourceRect = rect;
//cb.EndInit();
fb.BeginInit();
fb.Source = bi;
fb.DestinationFormat = PixelFormats.Gray2;
fb.EndInit();
// Properties must be set between BeginInit and EndInit calls.
tb.BeginInit();
tb.Source = fb;
// Set image rotation.
tb.Transform = new RotateTransform(90);
tb.EndInit();
// Set the Image source.
rotated90.Source = tb;
Content = rotated90;
}
示例12: Convert
/// <summary>
/// Converts a bitmap and returns the result.
/// </summary>
/// <param name="bitmap">The bitmap to convert.</param>
/// <returns>The result.</returns>
public BitmapSource Convert(BitmapSource bitmap)
{
BitmapSource result = bitmap;
if(bitmap.Format != _format)
result = new FormatConvertedBitmap(result, _format, null, 0);
if(Rotate != 0)
result = new TransformedBitmap(result, new RotateTransform(Rotate));
return result;
}
示例13: ResizeImage
/// <summary>
/// Resizes the given Image
/// </summary>
/// <param name="source"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static BitmapImage ResizeImage(BitmapImage source, int width, int height)
{
var target = new TransformedBitmap(
source,
new ScaleTransform(
width / source.Width * 96 / source.DpiX,
height / source.Height * 96 / source.DpiY,
0, 0));
var resizedFrame = BitmapFrame.Create(target);
return FrameToBitmap(resizedFrame);
}
示例14: EncodeColorAsync
public async Task EncodeColorAsync(byte[] colorData, BinaryWriter writer)
{
await Task.Run(() =>
{
var format = PixelFormats.Bgra32;
int stride = (int)this.Width * format.BitsPerPixel / 8;
var bmp = BitmapSource.Create(
this.Width,
this.Height,
96.0,
96.0,
format,
null,
colorData,
stride);
BitmapFrame frame;
if (this.Width != this.OutputWidth || this.Height != this.OutputHeight)
{
var transform = new ScaleTransform((double)this.OutputHeight / this.Height, (double)this.OutputHeight / this.Height);
var scaledbmp = new TransformedBitmap(bmp, transform);
frame = BitmapFrame.Create(scaledbmp);
}
else
{
frame = BitmapFrame.Create(bmp);
}
var encoder = new JpegBitmapEncoder()
{
QualityLevel = this.JpegQuality
};
encoder.Frames.Add(frame);
using (var jpegStream = new MemoryStream())
{
encoder.Save(jpegStream);
if (writer.BaseStream == null || writer.BaseStream.CanWrite == false)
return;
// Header
writer.Write(this.OutputWidth);
writer.Write(this.OutputHeight);
writer.Write((int)jpegStream.Length);
// Data
jpegStream.Position = 0;
jpegStream.CopyTo(writer.BaseStream);
}
});
}
示例15: Initialize
public async void Initialize()
{
_tempBitmapFile = await _page.GenerateThumbnail();
Uri uri = new Uri(_tempBitmapFile.FileName, UriKind.Absolute);
var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = uri;
image.EndInit();
TransformedBitmap transformed = new TransformedBitmap(image, new RotateTransform(0));
Image.Value = transformed;
IsGeneratingImage.Value = false;
}