c#指针怎么显示成图片
答案:2 悬赏:40 手机版
解决时间 2021-03-17 05:19
- 提问者网友:寂寞撕碎了回忆
- 2021-03-16 08:40
c#指针怎么显示成图片
最佳答案
- 五星知识达人网友:你可爱的野爹
- 2021-03-16 09:21
Bitmap src=new Bitmap(200,200);
BitmapData srcData = src.LockBits(rect, ImageLockMode.ReadWrite, src.PixelFormat);
byte* srcByte = (byte*)srcData.Scan0;
然后for循环把你的数据值放到指针srcByte里面,这样就显示成图片了
我这边有一个是把图片数据取出,你修改下就可以改为写入了。
private static byte[] GetPixels(Bitmap src, Rectangle rect)
{
if (src == null) return null;
int w = rect.Width;
int h = rect.Height;
int PixelCount = w * h;
int Depth = System.Drawing.Bitmap.GetPixelFormatSize(src.PixelFormat);
BitmapData srcData = null;
try
{
srcData = src.LockBits(rect, ImageLockMode.ReadWrite, src.PixelFormat);
int step = Depth / 8;
int stride = srcData.Stride;
int jumpStride = stride - w * step;
byte[] dst = null;
byte* srcByte = (byte*)srcData.Scan0;
dst = new byte[PixelCount * step];
int n = 0;
for (int j = 0; j < h; ++j)
{
for (int i = 0; i < w; ++i)
{
for (int t = 0; t < step; ++t)
dst[n++] = srcByte[t];
srcByte += step;
}
srcByte += jumpStride;
}
src.UnlockBits(srcData);
srcData = null;
return dst;
}
catch (Exception ex)
{
return null;
}
finally
{
if (srcData != null)
{
src.UnlockBits(srcData);
srcData = null;
}
}
}
BitmapData srcData = src.LockBits(rect, ImageLockMode.ReadWrite, src.PixelFormat);
byte* srcByte = (byte*)srcData.Scan0;
然后for循环把你的数据值放到指针srcByte里面,这样就显示成图片了
我这边有一个是把图片数据取出,你修改下就可以改为写入了。
private static byte[] GetPixels(Bitmap src, Rectangle rect)
{
if (src == null) return null;
int w = rect.Width;
int h = rect.Height;
int PixelCount = w * h;
int Depth = System.Drawing.Bitmap.GetPixelFormatSize(src.PixelFormat);
BitmapData srcData = null;
try
{
srcData = src.LockBits(rect, ImageLockMode.ReadWrite, src.PixelFormat);
int step = Depth / 8;
int stride = srcData.Stride;
int jumpStride = stride - w * step;
byte[] dst = null;
byte* srcByte = (byte*)srcData.Scan0;
dst = new byte[PixelCount * step];
int n = 0;
for (int j = 0; j < h; ++j)
{
for (int i = 0; i < w; ++i)
{
for (int t = 0; t < step; ++t)
dst[n++] = srcByte[t];
srcByte += step;
}
srcByte += jumpStride;
}
src.UnlockBits(srcData);
srcData = null;
return dst;
}
catch (Exception ex)
{
return null;
}
finally
{
if (srcData != null)
{
src.UnlockBits(srcData);
srcData = null;
}
}
}
全部回答
- 1楼网友:第四晚心情
- 2021-03-16 09:49
匿名的2b
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯