write a code in c to draw a 128x128 pixel image with each pixel value decided by the user and save the data in a png file, also the possibility to load the image and modify specific pixel in c code in csharp
Code to draw a 128x128 pixel image with user-defined values and save it as a PNG file:
#include<stdio.h>#include<stdlib.h>#include<png.h>#define WIDTH 128#define HEIGHT 128intmain(){
int x, y;
png_byte color_type = PNG_COLOR_TYPE_GRAY;
png_byte bit_depth = 8;
png_structp png_ptr;
png_infop info_ptr;
png_bytep * row_pointers;
// Allocate memory for image datauint8_t imageData[WIDTH][HEIGHT];
// Get user-defined pixel valuesfor (y = 0; y < HEIGHT; y++) {
for (x = 0; x < WIDTH; x++) {
printf("Enter pixel value for x=%d, y=%d: ", x, y);
scanf("%hhu", &imageData[x][y]);
}
}
// Open file for writing FILE *fp = fopen("image.png", "wb");
if (!fp) {
printf("[write_png_file] File could not be opened for writing\n");
return-1;
}
// Initialize PNG structures png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
printf("[write_png_file] png_create_write_struct failed\n");
return-1;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
printf("[write_png_file] png_create_info_struct failed\n");
return-1;
}
if (setjmp(png_jmpbuf(png_ptr))) {
printf("[write_png_file] Error during init_io\n");
return-1;
}
// Set image attributes png_set_IHDR(png_ptr, info_ptr, WIDTH, HEIGHT,
bit_depth, color_type, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
// Allocate rows row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * HEIGHT);
for (y = 0; y < HEIGHT; y++) {
row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(png_ptr,info_ptr));
for (x = 0; x < WIDTH; x++) {
// Copy pixel values to row row_pointers[y][x] = imageData[x][y];
}
}
// Write PNG data png_init_io(png_ptr, fp);
png_set_rows(png_ptr, info_ptr, row_pointers);
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
// Cleanup and close filefor (y = 0; y < HEIGHT; y++) {
free(row_pointers[y]);
}
free(row_pointers);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
printf("Image saved as image.png\n");
}
2259 chars
82 lines
Code to load the image and modify specific pixels:
main.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
namespacePixelManipulationExample{
classProgram {
staticvoidMain(string[] args) {
// Load image Bitmap image = new Bitmap("image.png");
// Modify specific pixels Console.WriteLine("Enter pixel coordinates to modify (comma-separated, q to quit):");
while (true)
{
string input = Console.ReadLine();
if (input == "q") break;
string[] values = input.Split(',');
int x = int.Parse(values[0]);
int y = int.Parse(values[1]);
Console.WriteLine("Current pixel value: " + image.GetPixel(x, y).R);
Console.Write("Enter new pixel value: ");
int newValue = int.Parse(Console.ReadLine());
image.SetPixel(x, y, Color.FromArgb(newValue, newValue, newValue));
}
// Save modified image image.Save("modified_image.png", ImageFormat.Png);
Console.WriteLine("Modified image saved as modified_image.png");
}
}
}