本文整理汇总了C#中System.Windows.Media.Imaging.FormatConvertedBitmap类的典型用法代码示例。如果您正苦于以下问题:C# FormatConvertedBitmap类的具体用法?C# FormatConvertedBitmap怎么用?C# FormatConvertedBitmap使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
FormatConvertedBitmap类属于System.Windows.Media.Imaging命名空间,在下文中一共展示了FormatConvertedBitmap类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RgbImageAdapter
public RgbImageAdapter(string path)
{
this.source = new BitmapImage(new Uri(path));
if (source.Format != PixelFormats.Bgra32)
{
source = new FormatConvertedBitmap(source, PixelFormats.Bgra32, null, 0);
}
int width = source.PixelWidth;
int height = source.PixelHeight;
RgbPixel[,] result = new RgbPixel[height, width];
byte[] byteArray = new byte[height * width * 4];
int stride = width * 4;
source.CopyPixels(byteArray, stride, 0);
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
result[i, j] = new RgbPixel
{
Blue = byteArray[(i * width + j) * 4 + 0],
Green = byteArray[(i * width + j) * 4 + 1],
Red = byteArray[(i * width + j) * 4 + 2],
Alpha = byteArray[(i * width + j) * 4 + 3],
};
}
}
image = new RgbImage(result);
}
示例2: RenderCurve
private byte[] RenderCurve(
ref RenderTargetBitmap rtb,
ref FormatConvertedBitmap fcb,
int width,
IEnumerable<IList<Point>> collection,
double thickness,
int miterLimit,
int height,
int stride)
{
if (rtb == null
|| rtb.PixelWidth != width
|| rtb.PixelHeight != height)
{
rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
fcb = new FormatConvertedBitmap(rtb, DrawingParameter.TargetPixelFormat, DrawingParameter.TargetBitmapPalette, 1);
}
else
{
rtb.Clear();
}
collection
.Select(it =>
{
var visual = _drawingVisualFactory();
visual.Draw(it, thickness, miterLimit, _logicalToScreenMapperFactory());
return visual;
})
.ForEachElement(rtb.Render);
var curvePixels = new byte[stride * height];
fcb.CopyPixels(curvePixels, stride, 0);
return curvePixels;
}
示例3: Button1_Click
public void Button1_Click(object sender, RoutedEventArgs e)
{
try
{
BitmapImage myBitmapImage = new BitmapImage();
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(@text1.Text);
myBitmapImage.EndInit();
FormatConvertedBitmap gray = new FormatConvertedBitmap(myBitmapImage, PixelFormats.Gray4, null, 0);
myImage.Source = gray;
var enc = new JpegBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(gray));
using (var s = File.Create("gray.jpg"))
enc.Save(s);
}
catch
{
text1.Text = "Не верно введен путь";
}
/* public void Button2_Click(object sender, RoutedEventArgs e)
{
BitmapImage myBitmapImage = new BitmapImage();
myBitmapImage.BeginInit();
myBitmapImage.UriSource=new Uri(myImage);
myBitmapImage.EndInit();
var enc = new JpegBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(gray));
using (var s = File.Create("gray.jpg"))
enc.Save(s);*/
}
示例4: SetSources
/// <summary>
/// Cashes original ImageSource, creates and caches greyscale ImageSource and greyscale opacity mask
/// </summary>
private void SetSources()
{
_sourceC = Source;
try
{
// create and cache greyscale ImageSource
_sourceG = new FormatConvertedBitmap(new BitmapImage(new Uri(TypeDescriptor.GetConverter(Source).ConvertTo(Source, typeof(string)) as string)),
PixelFormats.Gray16, null, 0);
// create Opacity Mask for greyscale image as FormatConvertedBitmap does not keep transparency info
_opacityMaskG = new ImageBrush(_sourceC);
_opacityMaskG.Opacity = 0.5;
this.Source = IsEnabled ? _sourceC : _sourceG;
this.OpacityMask = IsEnabled ? _opacityMaskC : _opacityMaskG;
InvalidateProperty(IsEnabledProperty);
}
catch
{
#if DEBUG
MessageBox.Show(String.Format("The ImageSource used cannot be greyed out.\nUse BitmapImage or URI as a Source in order to allow greyscaling.\nSource type used: {0}", Source.GetType().Name),
"Unsupported Source in GreyableImage", MessageBoxButton.OK, MessageBoxImage.Warning);
#endif // DEBUG
// in case greyscale image cannot be created set greyscale source to original Source
_sourceG = Source;
}
}
示例5: ReduceColorDepth
public MemoryStream ReduceColorDepth(Bitmap bmp)
{
var ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Png);
var bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();
var newFormatedBitmapSource = new FormatConvertedBitmap();
newFormatedBitmapSource.BeginInit();
newFormatedBitmapSource.Source = bi;
var myPalette = new BitmapPalette(bi, 256);
newFormatedBitmapSource.DestinationPalette = myPalette;
//Set PixelFormats
newFormatedBitmapSource.DestinationFormat = PixelFormats.Indexed8;
newFormatedBitmapSource.EndInit();
var pngBitmapEncoder = new PngBitmapEncoder();
pngBitmapEncoder.Interlace = PngInterlaceOption.Off;
pngBitmapEncoder.Frames.Add(BitmapFrame.Create(newFormatedBitmapSource));
var memstream = new MemoryStream();
pngBitmapEncoder.Save(memstream);
memstream.Position = 0;
return memstream;
}
示例6: OpenEntry
public override Stream OpenEntry(ArcFile arc, Entry entry)
{
var pent = entry as PnaEntry;
if (null == pent)
return base.OpenEntry (arc, entry);
ImageData image;
using (var input = arc.File.CreateStream (entry.Offset, entry.Size))
image = ImageFormat.Read (input);
var bitmap = image.Bitmap;
if (bitmap.Format.BitsPerPixel != 32)
{
bitmap = new FormatConvertedBitmap (bitmap, PixelFormats.Bgra32, null, 0);
}
int stride = bitmap.PixelWidth * 4;
var pixels = new byte[stride * bitmap.PixelHeight];
bitmap.CopyPixels (pixels, stride, 0);
// restore colors premultiplied by alpha
for (int i = 0; i < pixels.Length; i += 4)
{
int alpha = pixels[i+3];
if (alpha != 0 && alpha != 0xFF)
{
pixels[i] = (byte)(pixels[i] * 0xFF / alpha);
pixels[i+1] = (byte)(pixels[i+1] * 0xFF / alpha);
pixels[i+2] = (byte)(pixels[i+2] * 0xFF / alpha);
}
}
return TgaStream.Create (pent.Info, pixels);
}
示例7: 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);
}
}
}
示例8: Rasterize
public static bool[,] Rasterize(Point[] points, int width, int height)
{
Contract.Requires(points != null);
Contract.Requires(width > 0);
Contract.Requires(height > 0);
Contract.Requires(width % 8 == 0);
Contract.Ensures(Contract.Result<bool[,]>() != null);
Contract.Ensures(Contract.Result<bool[,]>().GetLength(0) == width);
Contract.Ensures(Contract.Result<bool[,]>().GetLength(1) == height);
var canvas = new Canvas { Background = Brushes.White, Width = width, Height = height };
var polygon = new Polygon { Stroke = Brushes.Black, Fill = Brushes.Black, StrokeThickness = 1, Points = new PointCollection(points) };
canvas.Children.Add(polygon);
RenderOptions.SetEdgeMode(canvas, EdgeMode.Aliased);
canvas.Measure(new Size(width, height));
canvas.Arrange(new Rect(0, 0, canvas.DesiredSize.Width, canvas.DesiredSize.Height));
var rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default);
rtb.Render(canvas);
var fmb = new FormatConvertedBitmap(rtb, PixelFormats.BlackWhite, null, 0);
var pixels = new byte[width * height / 8];
fmb.CopyPixels(pixels, width / 8, 0);
System.Collections.BitArray ba = new System.Collections.BitArray(pixels);
var result = new bool[width, height];
for (int i = 0, y = 0; y < height; ++y)
for (int x = 0; x < width; ++x, ++i)
result[x, y] = !ba[i];
return result;
}
示例9: MyImageReader
public MyImageReader(string fileName)
{
var filePath = Path.Combine(Environment.CurrentDirectory, fileName);
_bitmap = new FormatConvertedBitmap(new BitmapImage(new Uri("file://" + filePath)), System.Windows.Media.PixelFormats.Bgr32, null, 0);
_pixels = new uint[_bitmap.PixelHeight * _bitmap.PixelWidth];
_bitmap.CopyPixels(_pixels, sizeof(uint) * _bitmap.PixelWidth, 0);
}
示例10: setPixelFormat2
public static System.Drawing.Bitmap setPixelFormat2(BitmapSource bitmapsource, System.Drawing.Imaging.PixelFormat format)
{
//convert image format
var src = new System.Windows.Media.Imaging.FormatConvertedBitmap();
src.BeginInit();
src.Source = bitmapsource;
if (format == System.Drawing.Imaging.PixelFormat.Format1bppIndexed)
{
src.DestinationFormat = System.Windows.Media.PixelFormats.BlackWhite;
}
if (format == System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
{
src.DestinationFormat = System.Windows.Media.PixelFormats.Gray8;
}
if (format == System.Drawing.Imaging.PixelFormat.Format24bppRgb)
{
src.DestinationFormat = System.Windows.Media.PixelFormats.Bgr24;
}
if (format == System.Drawing.Imaging.PixelFormat.Format32bppRgb)
{
src.DestinationFormat = System.Windows.Media.PixelFormats.Bgr32;
}
src.EndInit();
//copy to bitmap
Bitmap bitmap = new Bitmap(src.PixelWidth, src.PixelHeight, format);
var data = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, format);
src.CopyPixels(System.Windows.Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
bitmap.UnlockBits(data);
return bitmap;
}
示例11: CreateThemedBitmapSource
public static BitmapSource CreateThemedBitmapSource(BitmapSource src, Color bgColor, bool isHighContrast)
{
unchecked {
if (src.Format != PixelFormats.Bgra32)
src = new FormatConvertedBitmap(src, PixelFormats.Bgra32, null, 0.0);
var pixels = new byte[src.PixelWidth * src.PixelHeight * 4];
src.CopyPixels(pixels, src.PixelWidth * 4, 0);
var bg = HslColor.FromColor(bgColor);
int offs = 0;
while (offs + 4 <= pixels.Length) {
var hslColor = HslColor.FromColor(Color.FromRgb(pixels[offs + 2], pixels[offs + 1], pixels[offs]));
double hue = hslColor.Hue;
double saturation = hslColor.Saturation;
double luminosity = hslColor.Luminosity;
double n1 = Math.Abs(0.96470588235294119 - luminosity);
double n2 = Math.Max(0.0, 1.0 - saturation * 4.0) * Math.Max(0.0, 1.0 - n1 * 4.0);
double n3 = Math.Max(0.0, 1.0 - saturation * 4.0);
luminosity = TransformLuminosity(hue, saturation, luminosity, bg.Luminosity);
hue = hue * (1.0 - n3) + bg.Hue * n3;
saturation = saturation * (1.0 - n2) + bg.Saturation * n2;
if (isHighContrast)
luminosity = ((luminosity <= 0.3) ? 0.0 : ((luminosity >= 0.7) ? 1.0 : ((luminosity - 0.3) / 0.4))) * (1.0 - saturation) + luminosity * saturation;
var color = new HslColor(hue, saturation, luminosity, 1.0).ToColor();
pixels[offs + 2] = color.R;
pixels[offs + 1] = color.G;
pixels[offs] = color.B;
offs += 4;
}
BitmapSource newImage = BitmapSource.Create(src.PixelWidth, src.PixelHeight, src.DpiX, src.DpiY, PixelFormats.Bgra32, src.Palette, pixels, src.PixelWidth * 4);
newImage.Freeze();
return newImage;
}
}
示例12: BitmapSourceLuminanceSource
/// <summary>
/// Initializes a new instance of the <see cref="BitmapSourceLuminanceSource"/> class.
/// </summary>
/// <param name="bitmap">The bitmap.</param>
public BitmapSourceLuminanceSource(BitmapSource bitmap)
: base(bitmap.PixelWidth, bitmap.PixelHeight)
{
switch (bitmap.Format.ToString())
{
case "Bgr24":
case "Bgr32":
CalculateLuminanceBGR(bitmap);
break;
case "Bgra32":
CalculateLuminanceBGRA(bitmap);
break;
case "Rgb24":
CalculateLuminanceRGB(bitmap);
break;
case "Bgr565":
CalculateLuminanceBGR565(bitmap);
break;
default:
// there is no special conversion routine to luminance values
// we have to convert the image to a supported format
bitmap = new FormatConvertedBitmap(bitmap, PixelFormats.Bgra32, null, 0);
CalculateLuminanceBGR(bitmap);
break;
}
}
示例13: SetIcon
internal void SetIcon(string fileName)
{
var bitmapSource = new BitmapImage();
bitmapSource.BeginInit();
bitmapSource.UriSource = new Uri(fileName);
// ensure that we resize to the correct dimensions
bitmapSource.DecodePixelHeight = 46;
bitmapSource.DecodePixelWidth = 46;
bitmapSource.EndInit();
var pbgra32Image = new FormatConvertedBitmap(bitmapSource, System.Windows.Media.PixelFormats.Pbgra32, null, 0);
var bmp = new System.Windows.Media.Imaging.WriteableBitmap(pbgra32Image);
var images = new List<BandIcon>() { bmp.ToBandIcon(), bmp.ToBandIcon() };
Strapp.SetImageList(Strapp.TileId, images, 0);
_tiles.UpdateStrapp(Strapp);
// this should refresh all bindings into the Strapp (in particular, the image)
NotifyPropertyChanged("Strapp");
}
示例14: ConvertToOtherPixelFormat
public static BitmapImage ConvertToOtherPixelFormat(BitmapSource source, PixelFormat format)
{
var newFormatedBitmapSource = new FormatConvertedBitmap();
newFormatedBitmapSource.BeginInit();
newFormatedBitmapSource.Source = source;
newFormatedBitmapSource.DestinationFormat = format;
newFormatedBitmapSource.EndInit();
return BitmapSourceToBitmapImage(newFormatedBitmapSource.Source);
}
示例15: GetFormatConvertedBitmap
public static FormatConvertedBitmap GetFormatConvertedBitmap(SWM.PixelFormat pf, BitmapSource bs)
{
FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap();
newFormatedBitmapSource.BeginInit();
newFormatedBitmapSource.Source = bs;
newFormatedBitmapSource.DestinationFormat = pf;
newFormatedBitmapSource.EndInit();
return newFormatedBitmapSource;
}