Tag: 位图

在C#中将WriteableBitmap转换为位图

有没有办法在C#中将WriteableBitmap转换为Bitmap ?

在.NET中加载Canon .CR2文件

我正在尝试使用C#处理Canon RAW .CR2文件。 我的代码如下: BitmapDecoder bmpDec = BitmapDecoder.Create(new Uri(origFile), BitmapCreateOptions.DelayCreation, BitmapCacheOption.None); BitmapEncoder bmpEnc = new BmpBitmapEncoder(); bmpEnc.Frames.Add(bmpDec.Frames[0]); Stream ms = new MemoryStream(); bmpEnc.Save(ms); Image srcImage = Bitmap.FromStream(ms); 前几行似乎毫无障碍地运行,但线路 bmEnc.Save(ms); 只是挂起而没有完成,也没有提出任何例外。 有人有这个成功吗?

减少位图调色板中颜色数量的最佳方法

我有一个包含很多不同颜色的位图。 但我需要用这种图像制作一个40色或更少颜色的方案。 这就是为什么我需要方法来减少位图调色板中的颜色数量。 我正在使用下面的代码为我的应用程序: using System.Windows.Media.Imaging; //BitmapSource ================================================== //Create palette of requied color. Pay heed to maxColorCount it //can have any value, but PixelFormats supports only 2,4,16 and 256 colors. var myPalette = new BitmapPalette(ConvertBitmap(b) as BitmapSource, 16); newFormatedBitmapSource.DestinationPalette = myPalette; //Set PixelFormats newFormatedBitmapSource.DestinationFormat = PixelFormats.Indexed8; newFormatedBitmapSource.EndInit(); b = BitmapFromSource(newFormatedBitmapSource); 它可以工作,但很多时候我会在较暗的颜色的大部分固体背景上收到带有明亮像素的图像: 我试着自己做:最近的4或9像素(正方形)并混合它。 我还尝试减少位图大小并“拉伸”位图。 最后我试图吸收最近的颜色。 但是我用上面的方法得到的最好的结果。 […]

创建位图时C#内存不足

我正在创建一个应用程序(Windows窗体),允许用户根据他们选择的位置截取屏幕截图(拖动到选择区域)。 我想添加一个放大的“预览窗格”,以便用户可以更精确地选择他们想要的区域(更大的像素)。 在mousemove事件中,我有以下代码…… private void falseDesktop_MouseMove(object sender, MouseEventArgs e) { zoomBox.Image = showZoomBox(e.Location); zoomBox.Invalidate(); bmpCrop.Dispose(); } private Image showZoomBox(Point curLocation) { Point start = new Point(curLocation.X – 50, curLocation.Y – 50); Size size = new Size(100, 90); Rectangle rect = new Rectangle(start, size); Image selection = cropImage(falseDesktop.Image, rect); return selection; } private static Bitmap bmpCrop; […]

从半透明位图创建GraphicsPath

我想创建一个GraphicsPath和一个Points列表,以形成位图的非透明区域的轮廓。 如果需要,我可以保证每个图像只有一个非透明像素的固体集合。 因此,例如,我应该能够沿着像素的边缘顺时针或逆时针记录点并执行完全闭环。 这种算法的速度并不重要。 但是,如果我可以在较小且不太复杂的GraphicsPath中跳过某些点来减少,那么结果点的效率是非常重要的。 我将列出下面的当前代码,该代码与大多数图像完美配合。 但是,一些更复杂的图像最终会出现以错误顺序连接的路径。 我想我知道为什么会这样,但我无法想出解决方案。 public static Point[] GetOutlinePoints(Bitmap image) { List outlinePoints = new List(); BitmapData bitmapData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); byte[] originalBytes = new byte[image.Width * image.Height * 4]; Marshal.Copy(bitmapData.Scan0, originalBytes, 0, originalBytes.Length); for (int x = 0; x < bitmapData.Width; x++) { for (int y […]

C#在图像或位图中裁剪圆圈

我确信它很容易,但似乎找不到任何有答案的人。 我有一个图像,我需要从该图像中切出一个圆或甚至其他形状。 我需要这个代码在.net c#中。 我在课堂上这样做,所以它不是wpf或winform。 我需要传递x和y pos以及圆的大小。 其他选择是AForge,ImageStatistics。 我需要得到一个圆圈(图像的一部分)并获得StdDev。 谢谢您的帮助。 安德鲁 – 更新到chrispost。 这是c#中的chrispost。 不像他那么干净,但却是一个开始。 public System.Drawing.Image x(string sourceFile, int circleUpperLeftX, int circleUpperLeftY, int circleDiameter) { Bitmap SourceImage = new Bitmap(System.Drawing.Image.FromFile(sourceFile)); Rectangle CropRect = new Rectangle(circleUpperLeftX, circleUpperLeftY, circleDiameter, circleDiameter); Bitmap CroppedImage = SourceImage.Clone(CropRect, SourceImage.PixelFormat); TextureBrush TB = new TextureBrush(CroppedImage); Bitmap FinalImage = new Bitmap(circleDiameter, circleDiameter); […]

PixelFormat.Format32bppArgb似乎有错误的字节顺序

我尝试从Bitmap(System.Drawing.Bitmap)获取所有字节值。 因此我锁定字节并复制它们: public static byte[] GetPixels(Bitmap bitmap){ if(bitmap-PixelFormat.Equals(PixelFormat.Format32.bppArgb)){ var argbData = new byte[bitmap.Width*bitmap.Height*4]; var bd = bitmap.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat); System.Runtime.InteropServices.Marshal.Copy(bd.Scan0, argbData, 0, bitmap.Width * bitmap.Height * 4); bitmap.UnlockBits(bd); } } 我用一个非常简单的2×2 PNG图像测试了这个图像,这个图像是我在Photoshop中创建的像素(红色,绿色,蓝色,白色)。 由于格式,我期望argbData中的以下值: 255 255 0 0 255 0 255 0 255 0 0 255 255 255 255 255 但我得到了: 0 […]

如何在WPF图像中显示位图

我想实现一个图像编辑程序,但我无法在WPF中显示Bitmap。 对于一般编辑,我需要一个位图。 但是我无法在图像中显示它。 private void MenuItemOpen_Click(object sender, RoutedEventArgs e) { OpenFileDialog openfiledialog = new OpenFileDialog(); openfiledialog.Title = “Open Image”; openfiledialog.Filter = “Image File|*.bmp; *.gif; *.jpg; *.jpeg; *.png;”; if (openfiledialog.ShowDialog() == true) { image = new Bitmap(openfiledialog.FileName); } } 我将带有OpenFileDialog的Image加载到Bitmap中。 现在我想在我的WPF中设置图片。 像这样: Image.Source = image; 我真的需要一个Bitmap来获得特殊像素的颜色! 我需要一个简单的代码剪切。 谢谢您的帮助!

如何在C#中从byte 创建bmp文件

我在TCP Client中收到一个byte []数组。该数组包含一个24位RGB位图文件。如何创建具有给定宽度,高度和数据的位图文件? 在C ++中我使用它 int WriteBitmapFile(const char *filename, int width, int height, unsigned char *imageData) { FILE *filePtr; // file pointer BITMAPFILEHEADER bitmapFileHeader; // bitmap file header BITMAPINFOHEADER bitmapInfoHeader; // bitmap info header DWORD imageIdx; // used for swapping RGB->BGR unsigned char tempRGB; // used for swapping // open file for writing binary mode […]

LockBits图像旋转方法不起作用?

大家好。 在厌倦了Get / Set Pixel和RotateTransfom的缓慢性能和古怪行为之后,我使用LockBits进行2D位图图像旋转。 所以这是我提出的代码,通过我的计算,它应该完美地工作。 它没有。 private static void InternalRotateImage(Bitmap originalBitmap, Bitmap rotatedBitmap, PointF centerPoint, float theta) { BitmapData originalData = originalBitmap.LockBits( new Rectangle(0, 0, originalBitmap.Width, originalBitmap.Height), ImageLockMode.ReadOnly, originalBitmap.PixelFormat); BitmapData rotatedData = rotatedBitmap.LockBits( new Rectangle(0, 0, rotatedBitmap.Width, rotatedBitmap.Height), ImageLockMode.WriteOnly, rotatedBitmap.PixelFormat); unsafe { byte[,] A = new byte[originalData.Height * 2, originalBitmap.Width * 2]; byte[,] R […]