English 中文(简体)
如何转换 C# 中的 i2c oled 的图像
原标题:How to Convert Images for i2c oled in C#
I have a C# WPF App and I want to add the function to select an icon or png and convert it, so it can be send as a string to an arduino. There I want to display the Image on a i2c oled Screen. There are a lot of Website which can do that, but I don´t find any code for C#. I Want some functions similar to this website: https://javl.github.io/image2cpp/
问题回答
Right click your Project > Select Manage NuGet Packages > select Browse > Typo System.Drawing.Common then Enter > Select System.Drawing.Common Item > Click Install Button ImageConverter.cs public class ImageConverter { public BitmapImage LoadImage(string imagePath) { BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.UriSource = new Uri(imagePath, UriKind.RelativeOrAbsolute); bitmapImage.EndInit(); // Optionally, you can freeze the BitmapImage for improved performance bitmapImage.Freeze(); return bitmapImage; } public byte[] BitmapImageToByteArray(BitmapImage bitmapImage) { if (bitmapImage == null) throw new ArgumentNullException("bitmapImage"); // Ensure format is Black and White (1bpp) FormatConvertedBitmap convertedBitmap = new FormatConvertedBitmap(bitmapImage, PixelFormats.BlackWhite, null, 0); int stride = (convertedBitmap.PixelWidth + 7) / 8; // Calculate stride (bytes per row) int height = convertedBitmap.PixelHeight; byte[] imageData = new byte[stride * height]; // Copy bitmap data to byte array convertedBitmap.CopyPixels(imageData, stride, 0); return imageData; } public BitmapImage ByteArrayToBitmapImage(byte[] imageData, int width, int height) { if (imageData == null) throw new ArgumentNullException("imageData"); // Create BitmapSource from byte array BitmapSource bitmapSource = BitmapSource.Create(width, height, 96, 96, PixelFormats.BlackWhite, null, imageData, width / 8); // Convert BitmapSource to BitmapImage BitmapImage bitmapImage = new BitmapImage(); using (MemoryStream stream = new MemoryStream()) { BitmapEncoder encoder = new PngBitmapEncoder(); // Use any suitable encoder encoder.Frames.Add(BitmapFrame.Create(bitmapSource)); encoder.Save(stream); bitmapImage.BeginInit(); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.StreamSource = stream; bitmapImage.EndInit(); } return bitmapImage; } } MainWindow.xaml MainWindow.xaml.cs public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); var imageConverter = new ImageConverter(); var imagePath = @"F:2022CustomerCardCustomerCardServerUICustomerCardServerResImagespngerrir.png"; // Replace with your image file path var bitmapImage = imageConverter.LoadImage(imagePath); var imageArrays = imageConverter.BitmapImageToByteArray(bitmapImage); // send imageArrays to Arduino via serial port or wifi var bitmapImageMonocrome = imageConverter.ByteArrayToBitmapImage(imageArrays, 240, 240); imageControl.Source = bitmapImageMonocrome; } } Input Output




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签