7个月前,我们开始学习C#和WPF,作为所有想要做一些图像处理的新手,我们遇到了这样的问题:
为什么有位图和BitmapSource?每种方法的优点是什么?
在我们的项目中,我们必须根据数据生成一个位图。速度对我们来说很重要。
我们从位图开始,因为它更容易(尤其是方法:get/set象素),大量的例子都有很好的记录。但是,我们发现了WPF中的转换问题,以打印位图。
因此,我们尝试了BitmapSource,这并不容易,因为不同的像素格式。但我们最终还是成功了。
我们比较了每一代的速度。使用SetPixel (位图)比处理字节数组(BitmapSource)要慢得多,但是处理字节数组意味着复杂:步幅、像素格式.
因此,我们肯定选择了BitmapSource。但是我们想序列化一些BitmapSource。BitmapSource是不可序列化的。因此,使用OnSerializing,我们在位图中转换了BitmapSource (可序列化)。
我们的结论是:
位图优势:
BitmapSource优势:
你看到其他点了吗?
为了说明和像我们这样的新手,下面是我们需要的一些有用的方法:
改划:
public static System.Windows.Media.Imaging.BitmapSource BitmapToBitmapSource(System.Drawing.Bitmap source)
{
using (MemoryStream memory = new MemoryStream())
{
source.Save(memory, ImageFormat.Png);
memory.Position = 0;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
return bitmapImage;
}
}
public static System.Drawing.Bitmap BitmapFromSource(BitmapSource source)
{
using (MemoryStream outStream = new MemoryStream())
{
BitmapEncoder enc = new PngBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(source));
enc.Save(outStream);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(outStream);
// return bitmap; <-- leads to problems, stream is closed/closing ...
return new Bitmap(bitmap);
}
}没有锁的图像打开:
public static BitmapImage LoadImage(string uri)
{
BitmapImage monImage = null;
if (uri != null)
{
BitmapImage image = new BitmapImage();
using (FileStream stream = File.OpenRead(uri))
{
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = stream;
image.EndInit();
}
monImage = image;
}
return monImage;
}调整BitmapSource大小:
public static BitmapImage BitmapImageFromBitmapSourceResized(BitmapSource bitmapSource, int newWidth)
{
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
MemoryStream memoryStream = new MemoryStream();
BitmapImage bImg = new BitmapImage();
encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
encoder.Save(memoryStream);
bImg.BeginInit();
bImg.StreamSource = new MemoryStream(memoryStream.ToArray());
bImg.DecodePixelWidth = newWidth;
bImg.EndInit();
memoryStream.Close();
return bImg;
}代:
public static int GetBytesPerPixel(BitmapSource bmp)
{
return (bmp.Format.BitsPerPixel + 7) / 8;
}
public static int GetStrideFromeBitmapSource(BitmapSource bmp)
{
return 4 * ((bmp.PixelWidth * GetBytesPerPixel(bmp) + 3) / 4);
}
public static byte[] GetBytesFromBitmapSource(BitmapSource bmp)
{
int height = bmp.PixelHeight;
int stride = GetStrideFromeBitmapSource(bmp);
byte[] pixels = new byte[height * stride];
bmp.CopyPixels(pixels, stride, 0);
return pixels;
}
public static int GetWidth(int stride, int bytesPerPixel)
{
int width = (int)(
(float)stride
/ (float)bytesPerPixel
);
return width;
}
public static int GetHeight(byte[] bits, int stride)
{
int height = (int)(
(float)bits.Length
/ (float)stride
);
return height;
}
public static void SetPixelRgb24(ref byte[] bits, int x, int y, int stride, Color c)
{
bits[x * 3 + y * stride] = c.R;
bits[x * 3 + y * stride + 1] = c.G;
bits[x * 3 + y * stride + 2] = c.B;
}
public static void SetPixelBgra32(ref byte[] bits, int x, int y, int stride, Couleur c)
{
bits[x * 4 + y * stride + 0] = c.B;
bits[x * 4 + y * stride + 1] = c.G;
bits[x * 4 + y * stride + 2] = c.R;
bits[x * 4 + y * stride + 3] = c.A;
}
public static int GetAverageValueOfPixel(ref byte[] bits, int x, int y, int stride, int bytesPerPixel)
{
int sum = 0;
for (var i = 0; i < bytesPerPixel; i++)
sum += bits[x * bytesPerPixel + y * stride + i];
return (int)
(
sum
* (255f / (255f * bytesPerPixel))
);
}快照到BitmapSource:
public static BitmapSource SnapShotToBitmap(this UIElement source, double zoomX, double zoomY)
{
try
{
DataObject dataObject = new DataObject();
double actualHeight = source.RenderSize.Height;
double actualWidth = source.RenderSize.Width;
if (actualHeight == 0)
actualHeight = 1;
if (actualWidth == 0)
actualWidth = 1;
double renderHeight = actualHeight * zoomY;
double renderWidth = actualWidth * zoomX;
RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)renderWidth, (int)renderHeight, 96, 96, PixelFormats.Pbgra32);
VisualBrush sourceBrush = new VisualBrush(source);
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
using (drawingContext)
{
drawingContext.PushTransform(new ScaleTransform(zoomX, zoomY));
drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(0, 0), new Point(actualWidth, actualHeight)));
}
renderTarget.Render(drawingVisual);
return renderTarget;
}
catch (Exception e)
{
throw new Exception(e);
}
}发布于 2014-07-22 13:29:14
我只想说,位图实际上提供了一种通过位图的LockBits方法进行像素操作的超快方法。如果您想通过手动设置像素来创建位图,这是创建位图的最快方法之一。注意,BitmapSource使用WIC,位图使用GDI+。因此,在加载或复制像素数据数组时不应该有任何差异(或至多是边际的),而且它也不是Bitmapsource或Bitmap的好处。
我还会增加对位图端的支持,因为它是一个非常老的结构,许多库都接受位图进行编辑。
我所看到的BitmapSource的唯一好处是它是WPF中图像的来源,并且可以很容易地使用。
https://stackoverflow.com/questions/24885977
复制相似问题