C# 生成字节/asm文件的映像
我正在做一个机器学习项目。以下是我的要求C# 生成字节/asm文件的映像,c#,image,C#,Image,我正在做一个机器学习项目。以下是我的要求 给定不同文件大小的*.bytes或.asm文件 生成文件的灰度(rgb)图像,其值应介于0-255之间 将所有图像调整为N x N像素 我正在尝试用C#生成图像(因为我比Python更熟悉它)。我有下面的代码,它试图创建图像,但我有两个问题 它使用字节[]的长度来确定宽度和高度,这意味着我必须找到字节[]的平方根来确定长度和宽度。我不想要这个,我想要它根据我给它的大小来创建图像 我不确定生成的图像是否正确,因为我从灰度图像中输入了一个字节[],但生成的图
static void Main(字符串[]args)
{
字符串fileRead=@“C:\Users\User\source\repos\ExeTobinary\Testing\grayscale.jpg”;
字符串fileSave=@“C:\Users\User\source\repos\ExeTobinary\Testing\Test.jpg”;
随机r=新随机();
整数宽度=1000;
整数高度=1000;
字节[]像素值=新字节[宽度*高度];
/*对于(int i=0;i
方法如下
public static Bitmap GetBitmap(byte[] _rawDataPresented, int _width, int _height, double scalingFactor)
{
Bitmap image = new Bitmap(_width, _height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed); //Format8bppIndexed
// konwersja palety idexed na skale szarosci
ColorPalette grayPalette = image.Palette;
Color[] entries = grayPalette.Entries;
for (int i = 0; i < 256; i++)
{
Color grayC = new Color();
grayC = Color.FromArgb((byte)i, (byte)i, (byte)i);
entries[i] = grayC;
}
image.Palette = grayPalette;
// wrzut binary data do bitmapy
BitmapData dataR = image.LockBits(new Rectangle(Point.Empty, image.Size), ImageLockMode.WriteOnly, image.PixelFormat);
Marshal.Copy(_rawDataPresented, 0, dataR.Scan0, _rawDataPresented.Length);
image.UnlockBits(dataR);
// skalowanie wielkosci
Size newSize = new Size((int)(image.Width * scalingFactor), (int)(image.Height * scalingFactor));
Bitmap scaledImage = new Bitmap(image, newSize);
string fileSave = @"C:\Users\User\source\repos\ExeTobinary\Testing\Test.jpg";
scaledImage.Save(fileSave, ImageFormat.Jpeg);
return scaledImage;
}
public static Bitmap GetBitmap(字节[]\u rawDataPresented,int\u width,int\u height,双比例因子)
{
位图图像=新位图(_-width,_-height,System.Drawing.Imaging.PixelFormat.format8bpindexed);//format8bpindexed
//我的名字叫斯凯尔·萨罗西
colorplate grayplate=image.palete;
颜色[]项=灰色调色板。项;
对于(int i=0;i<256;i++)
{
颜色灰度c=新颜色();
grayC=Color.FromArgb((字节)i,(字节)i,(字节)i);
分录[i]=grayC;
}
image.palete=灰色调色板;
//wrzut二进制数据做位图
BitmapData dataR=image.LockBits(新矩形(Point.Empty,image.Size),ImageLockMode.WriteOnly,image.PixelFormat);
Marshal.Copy(_rawDataPresented,0,dataR.Scan0,_rawDataPresented.Length);
图像.解锁位(dataR);
//斯卡洛瓦尼·维尔科西
大小newSize=新大小((int)(image.Width*缩放因子),(int)(image.Height*缩放因子));
位图缩放图像=新位图(图像,新闻大小);
字符串fileSave=@“C:\Users\User\source\repos\ExeTobinary\Testing\Test.jpg”;
保存(fileSave,ImageFormat.Jpeg);
返回缩放图像;
}
我在这里使用了灰度图像来测试该方法的有效性,但输入文件可能是*.bytes或.asm文件。做过类似事情的人能在这方面提供帮助吗。。。总而言之,我想从.bytes或.asm文件生成灰度图像,这些文件最终需要具有相同的维度。我更喜欢C#中的代码示例,但如果我得到python版本,我不会介意
这是我在测试中使用的灰度图像
这是我的结果
是jpg文件上具有ascii名称和原始大小的标题。您必须阅读标题并获取信息。请参见:
Color grayC=new Color()代码>← 不要这样做。您创建了一个新对象,但从不使用它,因为下一行会立即替换它。只需执行entries[i]=Color.FromArgb(i,i,i)代码>直接。Color.FromArgb
的参数是Int32
。请注意,当前方法忽略图像步长。因为,在任何宽度不是4的精确倍数的图像上,每行后面的填充都会丢失字节。每行复制一次以避免这种情况。有关从字节构建映像的代码,请参阅。
public static Bitmap GetBitmap(byte[] _rawDataPresented, int _width, int _height, double scalingFactor)
{
Bitmap image = new Bitmap(_width, _height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed); //Format8bppIndexed
// konwersja palety idexed na skale szarosci
ColorPalette grayPalette = image.Palette;
Color[] entries = grayPalette.Entries;
for (int i = 0; i < 256; i++)
{
Color grayC = new Color();
grayC = Color.FromArgb((byte)i, (byte)i, (byte)i);
entries[i] = grayC;
}
image.Palette = grayPalette;
// wrzut binary data do bitmapy
BitmapData dataR = image.LockBits(new Rectangle(Point.Empty, image.Size), ImageLockMode.WriteOnly, image.PixelFormat);
Marshal.Copy(_rawDataPresented, 0, dataR.Scan0, _rawDataPresented.Length);
image.UnlockBits(dataR);
// skalowanie wielkosci
Size newSize = new Size((int)(image.Width * scalingFactor), (int)(image.Height * scalingFactor));
Bitmap scaledImage = new Bitmap(image, newSize);
string fileSave = @"C:\Users\User\source\repos\ExeTobinary\Testing\Test.jpg";
scaledImage.Save(fileSave, ImageFormat.Jpeg);
return scaledImage;
}