Brightness and Contrast for a canvas image with javascript

前端 未结 4 1631
眼角桃花
眼角桃花 2021-02-03 10:23

I have an image in a tag

var img = new Image();
ctx.drawImage(img,0,0,img.width,img.height);
ecc...

How is possible to change the Brightness

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-03 10:57

    You can try this (c# code):

     Bitmap originalImage;
     Bitmap adjustedImage;
     double brightness = 1.0f; // no change in brightness
     double constrast = 2.0f; // twice the contrast
     double gamma = 1.0f; // no change in gamma
    
     float adjustedBrightness = brightness - 1.0f;
     float[][] ptsArray ={
                    new float[] {contrast, 0, 0, 0, 0}, // scale red
                    new float[] {0, contrast, 0, 0, 0}, // scale green
                    new float[] {0, 0, contrast, 0, 0}, // scale blue
                    new float[] {0, 0, 0, 1.0f, 0}, // don't scale alpha
                    new float[] {adjustedBrightness, adjustedBrightness, adjustedBrightness, 0, 1}};
    
     imageAttributes = new ImageAttributes();
     imageAttributes.ClearColorMatrix();
     imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
     imageAttributes.SetGamma(gamma, ColorAdjustType.Bitmap);
     Graphics g = Graphics.FromImage(adjustedImage);
     g.DrawImage(originalImage, new Rectangle(0,0,adjustedImage.Width,adjustedImage.Height)
                ,0,0,bitImage.Width,bitImage.Height,
     GraphicsUnit.Pixel, imageAttributes);
    

提交回复
热议问题