Задачка о буфере и цвете
222 сообщения
#13 лет назад
Есть такой способ буферизации большой картинки. это рабочий код но сделан для серой картинки.его вполне можно не читать целиком. ключевой вопрос вот в чем: я хочу сделать тоже самое для полноцветной.
но так как тут она у них серая - то они объявляют массив байтов, потом переносят нужную часть картинки в байтовый буфер а потом при желании -на экран.
А с полноцветной то ли не выходит, то ли я неправильный метод использую. изначально надо перенести картинку в байтовый массив из уже загруженной картинки пытаюсь сканировать попиксельно так:
Convert.ToByte( (mainMap.Image as Bitmap).GetPixel(x,y)
но не выходит ведь к примеру #FFFFFF это же больше чем байт. но вот как извернутся чтобы сохранить полноцветую картинку- не пойму.
исходный код с серой картинкой
using System;
02.
using System.Drawing;
03.
using System.Windows.Forms;
04.
using System.Drawing.Imaging;
05.
using System.Runtime.InteropServices;
06.
07.
namespace WindowsFormsApplication72
08.
{
09.
public partial class Form1 : Form
10.
{
11.
RawBitmap rawBitmap;
12.
Bitmap bufferBmp;
13.
14.
public Form1()
15.
{
16.
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
17.
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
18.
SetStyle(ControlStyles.UserPaint, true);
19.
20.
//создаем буферный битмап, размером с экран
21.
var screenSize = Screen.PrimaryScreen.Bounds.Size;
22.
bufferBmp = new Bitmap(screenSize.Width, screenSize.Height, PixelFormat.Format8bppIndexed);
23.
//формируем палитру оттенков серого
24.
var p = bufferBmp.Palette;
25.
for (int i = 0; i < 256; i++)
26.
p.Entries = Color.FromArgb(i, i, i);
27.
bufferBmp.Palette = p;
28.
29.
//создаем некое серое изображение, представленное массивом пикселей 16480x2472
30.
rawBitmap = new RawBitmap(16480, 2472);
31.
for(int x = 0;x<rawBitmap.pixels.GetLength(0);x++)
32.
for (int y = 0; y < rawBitmap.pixels.GetLength(1); y++)
33.
rawBitmap.pixels = (byte)Math.Round((Math.Sin(x / 50f)+y/50f)*127);
34.
//
35.
AutoScroll = true;
36.
AutoScrollMinSize = new System.Drawing.Size(rawBitmap.width, rawBitmap.height);
37.
}
38.
39.
40.
protected override void OnPaintBackground(PaintEventArgs e)
41.
{
42.
//base.OnPaintBackground(e);
43.
}
44.
45.
protected override void OnPaint(PaintEventArgs e)
46.
{
47.
//рисуем rawBitmap в буфер
48.
rawBitmap.Draw(bufferBmp, new Rectangle(HorizontalScroll.Value, VerticalScroll.Value, bufferBmp.Width, bufferBmp.Height));
49.
//рисуем буфер
50.
e.Graphics.DrawImage(bufferBmp, 0, 0);
51.
}
52.
}
53.
54.
//изображение, представленное массивом пикселей
55.
public class RawBitmap
56.
{
57.
public readonly byte pixels;
58.
public readonly int width;
59.
public readonly int height;
60.
61.
public RawBitmap(int width, int height)
62.
{
63.
this.width = width;
64.
this.height = height;
65.
pixels = new byte;
66.
}
67.
68.
//отрисовка фрагмента в буферный битмап
69.
public void Draw(Bitmap bmp, Rectangle rect)
70.
{
71.
BitmapData srcBitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);
72.
byte srcData = new byte;
73.
for (int y = 0; y < bmp.Height; y++)
74.
{
75.
int start = y * srcBitmapData.Stride;
76.
for (int x = 0; x < srcBitmapData.Stride; x += 1)
77.
if (rect.Left + x < width && rect.Top + y < height)
78.
srcData = pixels;
79.
else
80.
srcData = 0;
81.
}
82.
Marshal.Copy(srcData, 0, srcBitmapData.Scan0, srcData.Length);
83.
bmp.UnlockBits(srcBitmapData);
84.
}
85.
}
86.
}