本文整理汇总了C#中System.Windows.Media.Imaging.BitmapImage.CopyPixels方法的典型用法代码示例。如果您正苦于以下问题:C# BitmapImage.CopyPixels方法的具体用法?C# BitmapImage.CopyPixels怎么用?C# BitmapImage.CopyPixels使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Media.Imaging.BitmapImage
的用法示例。
在下文中一共展示了BitmapImage.CopyPixels方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetSinglePixel
static Color GetSinglePixel(BitmapImage source, int x, int y)
{
int stride = (source.Format.BitsPerPixel + 7) / 8;
byte[] pixels = new byte[stride];
source.CopyPixels(new Int32Rect(x, y, 1, 1), pixels, stride, 0);
return Color.FromArgb(pixels[3], pixels[0], pixels[1], pixels[2]);
}
示例2: openButton_Click
private void openButton_Click(object sender, RoutedEventArgs e)
{
var dlg = new OpenFileDialog();
if (dlg.ShowDialog(this).Value)
{
try
{
if (new FileInfo(dlg.FileName).Extension.ToLower() == ".bmpc")
{
var image = new ImageBMPC(dlg.FileName, false, null);
var writable = new WriteableBitmap(image.Size.Width, image.Size.Height, 96, 96, PixelFormats.Bgra32, null);
int stride = (image.Size.Width * 32 + 7) / 8;
// Flip RB Color bits
var data = new byte[image.Mipmaps[0].Data.LongLength];
image.Mipmaps[0].Data.CopyTo(data, 0);
for (int i2 = 0; i2 != data.Length; i2 += 4)
{
byte c = data[i2];
data[i2] = data[i2+2];
data[i2+2] = c;
}
writable.WritePixels(new Int32Rect(0, 0, image.Size.Width, image.Size.Height), data, stride, 0);
dstImage.Source = writable;
}
else
{
// Load binary data
var bitmap = new BitmapImage(new Uri(dlg.FileName, UriKind.Absolute));
int stride = (bitmap.PixelWidth * bitmap.Format.BitsPerPixel + 7) / 8;
srcData = new byte[bitmap.PixelWidth * stride];
dstData = new byte[bitmap.PixelWidth * stride];
bitmap.CopyPixels(srcData, stride, 0);
bitmap.CopyPixels(dstData, stride, 0);
// Get image atributes
var ext = new FileInfo(dlg.FileName).Extension.ToLower();
width = bitmap.PixelWidth;
height = bitmap.PixelHeight;
// Display images
var w = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight, 96, 96, PixelFormats.Bgra32, null);
w.WritePixels(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight), srcData, stride, 0);
dstImage.Source = w;
srcImage.Source = bitmap;
processesImage();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
示例3: btnFilter_Click
private void btnFilter_Click(object sender, RoutedEventArgs e)
{
if (_pointCollection[0].X != 0 || _pointCollection[_pointCollection.Count - 1].X != 255)
{
MessageBox.Show("You have to set the initial and final values of X!");
return;
}
if (imgOriginal.Source == null)
{
MessageBox.Show("You have to load the image first!");
return;
}
ImageSource ims = imgOriginal.Source;
_bitmapImage = (BitmapImage)ims;
_height = _bitmapImage.PixelHeight;
_width = _bitmapImage.PixelWidth;
_rawStride = (_width * _bitmapImage.Format.BitsPerPixel + 7) / 8;
_pixelData = new byte[_rawStride * _height];
_bitmapImage.CopyPixels(_pixelData, _rawStride, 0);
int[] function = new int[256];
for (int i = 0; i < 256; i++)
{
bool isKnown = false;
foreach (Point p in _pointCollection)
{
if (i == (int)p.X)
{
isKnown = true;
function[i] = (int)(255 - p.Y);
break;
}
}
if (isKnown)
{
continue;
}
for (int j = 0; j < _pointCollection.Count; j++)
{
if (_pointCollection[j].X < i && _pointCollection[j + 1].X >= i)
{
function[i] = 255 - ((int)_pointCollection[j].Y + ((int)_pointCollection[j + 1].Y - (int)_pointCollection[j].Y) * (i - (int)_pointCollection[j].X) / ((int)_pointCollection[j + 1].X - (int)_pointCollection[j].X));
break;
}
}
}
for (int y = 0; y < _height; y++)
{
int yIndex = y * _rawStride;
for (int x = 0; x < _rawStride; x+=3)
{
if (x+yIndex+2 > _pixelData.Length-1)
break;
_pixelData[x + yIndex] = (byte)function[_pixelData[x + yIndex]];
_pixelData[x + yIndex + 1] = (byte)function[_pixelData[x + yIndex + 1]];
_pixelData[x + yIndex + 2] = (byte)function[_pixelData[x + yIndex + 2]];
}
}
_filteredBitmap = BitmapSource.Create(_width, _height, 96, 96, _bitmapImage.Format, _bitmapImage.Palette, _pixelData, _rawStride);
imgFiltered.Source = _filteredBitmap;
}
示例4: GetHashCode
public List<string> GetHashCode(BitmapImage bitmap)
{//takes a bitmap and translates it into the hashcode list
List<string> hashCode= new List<string>();
int stride = bitmap.PixelWidth * (bitmap.Format.BitsPerPixel / 8);
for (int i = 0; i < bitmap.PixelHeight; i++)//divides an image into rows
{
string row="";
for (int x = 0; x < bitmap.PixelWidth; x++)//iterates through each pixel in the row
{
byte[] pixel = new byte[bitmap.PixelHeight];//holds color values of a single pixel
bitmap.CopyPixels(new Int32Rect(x, i, 1, 1), pixel, stride, 0);//assigns color values of a single pixel to the pixel array
Color singlePixel = new Color();//creates new color objects and assigns the color values found in pixel array to it
singlePixel.B = pixel[0];
singlePixel.G = pixel[1];
singlePixel.R = pixel[2];
singlePixel.A = pixel[3];
row += singlePixel.GetHashCode().ToString();//converst the color value into the hashcode and converts it to the string
}
hashCode.Add(row);
if (i % 20 == 0)//Updates programm only after each tenth iteration, this makes program run a bit faster
UpdatePRogress();
}
return hashCode;
}
示例5: BitmapImageToBitmap
public static System.Drawing.Bitmap BitmapImageToBitmap(BitmapImage source)
{
Bitmap bmp = new Bitmap(source.PixelWidth, source.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
BitmapData data = bmp.LockBits(new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size), ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
source.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
bmp.UnlockBits(data);
return bmp;
/*
System.Drawing.Bitmap btm = null;
int width = srs.PixelWidth;
int height = srs.PixelHeight;
int stride = width * ((srs.Format.BitsPerPixel + 7) / 8);
byte[] bits = new byte[height * stride];
srs.CopyPixels(bits, stride, 0);
unsafe
{
fixed (byte* pB = bits)
{
IntPtr ptr = new IntPtr(pB);
btm = new System.Drawing.Bitmap(width, height, stride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, ptr);
}
}
return btm;*/
}
示例6: ConvertBitmapImageToByteArray
private byte[] ConvertBitmapImageToByteArray(BitmapImage bitmap)
{
int stride = bitmap.PixelWidth * 4;
int size = bitmap.PixelHeight * stride;
byte[] pixels = new byte[size];
bitmap.CopyPixels(pixels, stride, 0);
return pixels;
}
示例7: ReadAllBytes
private byte[] ReadAllBytes(BitmapImage img)
{
var stride = img.PixelWidth * (img.Format.BitsPerPixel / 8);
var pixels = new byte[stride * img.PixelHeight];
img.CopyPixels(pixels, stride, 0);
return pixels;
}
示例8: ConvertBitmapToOneTrueDpi
public static BitmapSource ConvertBitmapToOneTrueDpi(BitmapImage bitmapImage) {
var width = bitmapImage.PixelWidth;
var height = bitmapImage.PixelHeight;
var stride = width * bitmapImage.Format.BitsPerPixel;
var pixelData = new byte[stride * height];
bitmapImage.CopyPixels(pixelData, stride, 0);
return BitmapSource.Create(width, height, OneTrueDpi, OneTrueDpi, bitmapImage.Format, null, pixelData, stride);
}
示例9: LoadPixelData
/// <summary>
/// Obtains the image data once it is loaded
/// </summary>
private void LoadPixelData()
{
var bitmapImage = new BitmapImage(new Uri("pack://application:,,,/Examples/HistogramDemo/hare.jpg"));
int stride = ((bitmapImage.PixelWidth * bitmapImage.Format.BitsPerPixel) + 7) / 8;
var pixelByteArray = new byte[bitmapImage.PixelHeight * stride];
bitmapImage.CopyPixels(pixelByteArray, stride, 0);
this.pixelData = new int[pixelByteArray.Length / 4];
Buffer.BlockCopy(pixelByteArray, 0, this.pixelData, 0, pixelByteArray.Length);
}
示例10: MakeBitmap
public ImageContext MakeBitmap()
{
BitmapImage myBitmapImage = new BitmapImage();
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(@text1.Text);
myBitmapImage.EndInit();
myImage.Source = myBitmapImage;
ImageContext inSource = new ImageContext(myBitmapImage.PixelHeight, myBitmapImage.PixelWidth, myBitmapImage.Format);
myBitmapImage.CopyPixels(inSource.PixelByteArray, inSource.NStride, 0);
return inSource;
}
示例11: Load
public byte[] Load(string path, out int width, out int height, out int stride)
{
var source = new BitmapImage(new Uri("pack://application:,,,/" + path));
width = source.PixelWidth;
height = source.PixelHeight;
stride = 500;
var bytes = new byte[stride * height];
source.CopyPixels(bytes, stride, 0);
return bytes;
}
示例12: ConvertBitmapTo96Dpi
internal static BitmapSource ConvertBitmapTo96Dpi(BitmapImage bitmap_image)
{
const double dpi = 96;
var width = bitmap_image.PixelWidth;
var height = bitmap_image.PixelHeight;
var stride = (bitmap_image.Format.BitsPerPixel * width + 7) / 8;
var pixel_data = new byte[stride * height];
bitmap_image.CopyPixels(pixel_data, stride, 0);
var rtn = BitmapSource.Create(width, height, dpi, dpi, bitmap_image.Format, null, pixel_data, stride);
if (rtn.CanFreeze)
rtn.Freeze();
return rtn;
}
示例13: ProcessImage
private void ProcessImage(BitmapImage bmp)
{
byte[] pixels = new byte[bmp.PixelWidth * bmp.PixelHeight * 4];
bmp.CopyPixels(pixels, bmp.PixelWidth * 4, 0);
for (int i = 0; i < pixels.Length; )
{
//BGRA
blues[pixels[i++]]++;
greens[pixels[i++]]++;
reds[pixels[i++]]++;
i++;
}
CreateHistograms();
}
示例14: MakeBitmap
public void MakeBitmap()
{
BitmapImage myBitmapImage = new BitmapImage();
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(@text1.Text);
myBitmapImage.EndInit();
myImage.Source = myBitmapImage;
//
height = myBitmapImage.PixelHeight;
width = myBitmapImage.PixelWidth;
imageFormat = myBitmapImage.Format;
nStride = (myBitmapImage.PixelWidth * imageFormat.BitsPerPixel + 7) / 8;
pixelByteArraySize = myBitmapImage.PixelHeight * nStride;
pixelByteArrayIn = new byte[pixelByteArraySize];
myBitmapImage.CopyPixels(pixelByteArrayIn, nStride, 0);
}
示例15: LoadImage
private void LoadImage()
{
this.originalImage = new BitmapImage();
originalImage.BeginInit();
originalImage.UriSource = new Uri(FilePath.Text, UriKind.Absolute);
originalImage.EndInit();
OriginalImage.Source = originalImage;
ModifiedImage.Source = originalImage;
int[] pixelData = new int[originalImage.PixelHeight*originalImage.PixelWidth];
int widthInByte = 4*originalImage.PixelWidth;
originalImage.CopyPixels(pixelData, widthInByte, 0);
DrawHistogram(pixelData, "ORIGINAL");
DrawHistogram(pixelData, "MODIFIED");
}