c# - Read jpg, edit pixel and save without loss -


i writing program can load jpg file, , edit pixel 0,0 color red , save jpg without loss. it's possible?

my program exception on line propertyitem propitem = image1.getpropertyitem(20624);, don't know why.

error is:

an unhandled exception of type 'system.argumentexception' occurred in system.drawing.dll

code

image image1 = image.fromfile("1789594.jpg");  bitmap bitmap1 = new bitmap(image1); bitmap1.getpixel(0, 0); color pixelcolor = bitmap1.getpixel(0, 0); console.writeline(pixelcolor.r + " - " + pixelcolor.g + " - " + pixelcolor.b); console.readline(); color redcolor = color.fromargb(255, 0, 0); bitmap1.setpixel(0, 0, redcolor); image1 = (image)bitmap1; // propertyitem image1. because propertyitem not // have public constructor, first need existing propertyitem propertyitem propitem = image1.getpropertyitem(20624);  // change id of propertyitem. propitem.id = 20625;  // set new propertyitem image1. image1.setpropertyitem(propitem);  // save image. image1.save("outputcsharp.jpeg", imageformat.jpeg); //jpg 

i going answer jpeg perspective, not c#. there no way edit pixels in jpeg , save no loss.

the jpeg process has several steps introduce loss.

you can minimize changes 1) using same sampling; , 2) same quantization tables in original source file.

one of changes in jpeg converts ycbcr color space before compression. there not 1-to-1 mapping between rgb , ycbcr. can set rgb pixel values, compress, expand , find different transformation.

i know nothing c# but, in documentation propertyid 20624 luminance quantization table. looks code trying make chrominance quantization table.

that's have bizarre effects if achieve result.

i come , add c# guess---

bitmap bitmap1 = new bitmap(image1); 

c# might think image1 jpeg , bitmap1 not jpeg. thus, trying retrieve jpeg specific attributes non-jpeg image causes argument error.


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -