Skip to content

Commit 5d007ab

Browse files
committed
bitmap scroll/roll example
1 parent e657cad commit 5d007ab

File tree

13 files changed

+737
-1
lines changed

13 files changed

+737
-1
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{22482F88-CE61-47F9-B216-45A67AAB62E7}</ProjectGuid>
8+
<OutputType>WinExe</OutputType>
9+
<RootNamespace>_02_bitmap_scroll</RootNamespace>
10+
<AssemblyName>02 bitmap scroll</AssemblyName>
11+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="System" />
36+
<Reference Include="System.Core" />
37+
<Reference Include="System.Xml.Linq" />
38+
<Reference Include="System.Data.DataSetExtensions" />
39+
<Reference Include="Microsoft.CSharp" />
40+
<Reference Include="System.Data" />
41+
<Reference Include="System.Deployment" />
42+
<Reference Include="System.Drawing" />
43+
<Reference Include="System.Net.Http" />
44+
<Reference Include="System.Windows.Forms" />
45+
<Reference Include="System.Xml" />
46+
</ItemGroup>
47+
<ItemGroup>
48+
<Compile Include="Form1.cs">
49+
<SubType>Form</SubType>
50+
</Compile>
51+
<Compile Include="Form1.Designer.cs">
52+
<DependentUpon>Form1.cs</DependentUpon>
53+
</Compile>
54+
<Compile Include="Program.cs" />
55+
<Compile Include="Properties\AssemblyInfo.cs" />
56+
<EmbeddedResource Include="Form1.resx">
57+
<DependentUpon>Form1.cs</DependentUpon>
58+
</EmbeddedResource>
59+
<EmbeddedResource Include="Properties\Resources.resx">
60+
<Generator>ResXFileCodeGenerator</Generator>
61+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
62+
<SubType>Designer</SubType>
63+
</EmbeddedResource>
64+
<Compile Include="Properties\Resources.Designer.cs">
65+
<AutoGen>True</AutoGen>
66+
<DependentUpon>Resources.resx</DependentUpon>
67+
</Compile>
68+
<None Include="Properties\Settings.settings">
69+
<Generator>SettingsSingleFileGenerator</Generator>
70+
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
71+
</None>
72+
<Compile Include="Properties\Settings.Designer.cs">
73+
<AutoGen>True</AutoGen>
74+
<DependentUpon>Settings.settings</DependentUpon>
75+
<DesignTimeSharedInput>True</DesignTimeSharedInput>
76+
</Compile>
77+
</ItemGroup>
78+
<ItemGroup>
79+
<None Include="App.config" />
80+
</ItemGroup>
81+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
82+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
5+
</startup>
6+
</configuration>

projects/18-01-10_fast_pixel_bitmap/WindowsFormsApp1/02 bitmap scroll/Form1.Designer.cs

Lines changed: 121 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Data;
5+
using System.Drawing;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using System.Windows.Forms;
10+
11+
using System.Drawing.Imaging; // for ImageLockMode
12+
using System.Runtime.InteropServices; // for Marshal
13+
14+
namespace _02_bitmap_scroll
15+
{
16+
public partial class Form1 : Form
17+
{
18+
private static int update_count = 0;
19+
private static Random rand = new Random();
20+
21+
private static List<List<double>> data; // a list of column pixel values
22+
23+
public Form1()
24+
{
25+
InitializeComponent();
26+
Data_init();
27+
}
28+
29+
/// <summary>
30+
/// return a list of random doubles from 0.0 to 1.0
31+
/// </summary>
32+
private List<double> Generate_list_random(int itemCount) {
33+
List<double> list = new List<double>(itemCount);
34+
for (int i=0; i< itemCount; i++)
35+
{
36+
list.Add(rand.NextDouble());
37+
}
38+
return list;
39+
}
40+
41+
42+
/// <summary>
43+
/// fill "data" with 2d random data
44+
/// </summary>
45+
private void Data_init()
46+
{
47+
int data_samples = pictureBox1.Width;
48+
int data_size = pictureBox1.Height;
49+
50+
data = new List<List<double>>();
51+
for (int i=0; i<data_samples; i++)
52+
{
53+
data.Add(Generate_list_random(data_size));
54+
}
55+
56+
System.Console.WriteLine("generated random data");
57+
58+
}
59+
60+
private void Bitmap_from_data()
61+
{
62+
// create a bitmap we will work with
63+
Bitmap bitmap = new Bitmap(data.Count, data[0].Count, PixelFormat.Format8bppIndexed);
64+
65+
// modify the indexed palette to make it grayscale
66+
ColorPalette pal = bitmap.Palette;
67+
for (int i = 0; i < 256; i++)
68+
pal.Entries[i] = Color.FromArgb(255, i, i, i);
69+
bitmap.Palette = pal;
70+
71+
// prepare to access data via the bitmapdata object
72+
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
73+
ImageLockMode.ReadOnly, bitmap.PixelFormat);
74+
75+
// create a byte array to reflect each pixel in the image
76+
byte[] pixels = new byte[bitmapData.Stride * bitmap.Height];
77+
78+
// fill pixel array with data
79+
for (int col=0; col<data.Count; col++)
80+
{
81+
for (int row=0; row<data[col].Count; row++)
82+
{
83+
int bytePosition = row * bitmapData.Stride + col;
84+
pixels[bytePosition] = (byte)(255 * data[col][row]);
85+
}
86+
}
87+
88+
// turn the byte array back into a bitmap
89+
Marshal.Copy(pixels, 0, bitmapData.Scan0, pixels.Length);
90+
bitmap.UnlockBits(bitmapData);
91+
92+
// apply the bitmap to the picturebox
93+
pictureBox1.Image = bitmap;
94+
95+
}
96+
97+
private void Bitmap_roll()
98+
{
99+
data.Insert(data.Count, data[0]);
100+
data.RemoveAt(0);
101+
}
102+
103+
private void timer1_Tick(object sender, EventArgs e)
104+
{
105+
for (int i=0; i<5; i++)
106+
{
107+
Bitmap_roll();
108+
}
109+
110+
Bitmap_from_data();
111+
update_count += 1;
112+
label4.Text = string.Format("Update count: {0}", update_count);
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)