Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
invert color image in MVC C#


What I have tried:

[HttpPost]
        public JsonResult InvertColorMatrix(Image imgSource, string directoryPath)
        {
            string imageFullPath = String.Empty;
            int currentOffSet = 0, imageLength = 0;
            //DataCorrectionViewModel listDCViewModel = (DataCorrectionViewModel)TempData.Peek("ListDCViewModel");
            DataCorrectionViewModel listDCViewModel = (DataCorrectionViewModel)TempData.Peek("ListDCViewModel");
            DCImageViewModel imageViewModel = listDCViewModel.Item.ImageData;

            //directoryPath = @"C:\RL520DAT\Images";
            directoryPath = string.IsNullOrEmpty(directoryPath) ? "C:\\RL520DAT\\Images" : directoryPath;
              
               imageFullPath = directoryPath + "\\" + imageViewModel.Image_Path.ToString() + ".FIM";
               imageLength = Convert.ToInt32(imageViewModel.Cap_Front_Image_Size);
               currentOffSet = Convert.ToInt32(imageViewModel.Image_Offset);
                
                //Bitmap bmpDest = new Bitmap(imgSource.Width, imgSource.Height);
                Bitmap bmpDest = new Bitmap(imgSource.Width, imgSource.Height);
            ColorMatrix clrMatrix = new ColorMatrix(new float[][] {
            new float[] { -1, 0, 0, 0, 0 },
            new float[] { 0, -1, 0, 0, 0 },
            new float[] { 0, 0, -1, 0, 0 },
            new float[] { 0, 0, 0, 1, 0 },
            new float[] { 1, 1, 1, 0, 1 }
    });

            using (ImageAttributes attrImage = new ImageAttributes())
            {
                attrImage.SetColorMatrix(clrMatrix);

                using (Graphics g = Graphics.FromImage(bmpDest))
                {
                    g.DrawImage(imgSource, new Rectangle(0, 0, imgSource.Width, imgSource.Height), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel, attrImage);
                }
            }
            //return bmpDest;
            return Json(bmpDest, JsonRequestBehavior.AllowGet);
        }
Posted
Updated 18-Nov-19 1:27am
Comments
F-ES Sitecore 18-Nov-19 4:58am    
What's your question?

1 solution

Try this example from c# - Inverting image returns a black image - Stack Overflow[^] :
public Bitmap Transform(Bitmap source)
{
    //create a blank bitmap the same size as original
    Bitmap newBitmap = new Bitmap(source.Width, source.Height);

    //get a graphics object from the new image
    Graphics g = Graphics.FromImage(newBitmap);

    // create the negative color matrix
    ColorMatrix colorMatrix = new ColorMatrix(new float[][]
    {
new float[] {-1, 0, 0, 0, 0},
new float[] {0, -1, 0, 0, 0},
new float[] {0, 0, -1, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {1, 1, 1, 0, 1}
    });

    // create some image attributes
    ImageAttributes attributes = new ImageAttributes();

    attributes.SetColorMatrix(colorMatrix);

    g.DrawImage(source, new Rectangle(0, 0, source.Width, source.Height),
                0, 0, source.Width, source.Height, GraphicsUnit.Pixel, attributes);

    //dispose the Graphics object
    g.Dispose();

    return newBitmap;
}

I tested it with a JPG and it works for me ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900