The name "NewBitmap" does not exist in current context - c#

I am trying to make to put adjust brightness thing but I am getting does not exist in current context error for "NewBitmap" in this code
picBox.Image = AdjustBrightness(NewBitmap, trackBar1.Value);
Here is my code
private void trackBar1_Scroll(object sender, EventArgs e)
{
lblBrightNum.Text = trackBar1.Value.ToString();
picBox.Image = AdjustBrightness(NewBitmap, trackBar1.Value);
}
public static Bitmap AdjustBrightness(Bitmap Image, int Value)
{
Bitmap TempBitmap = (Bitmap)Image.Clone();
float FinalValue = (float)Value / 255.0f;
Bitmap NewBitmap = new Bitmap(TempBitmap.Width, TempBitmap.Height);
Graphics NewGraphics = Graphics.FromImage(NewBitmap);
float[][] FloatColorMatrix ={
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[] {FinalValue, FinalValue, FinalValue, 1, 1}
};
ColorMatrix NewColorMatrix = new ColorMatrix(FloatColorMatrix);
ImageAttributes Attributes = new ImageAttributes();
Attributes.SetColorMatrix(NewColorMatrix);
NewGraphics.DrawImage(TempBitmap, new Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, GraphicsUnit.Pixel, Attributes);
Attributes.Dispose();
NewGraphics.Dispose();
return NewBitmap;
}

Your program mainly contains two methods, one is trackBar1_Scroll, another one is AdjustBrightness, visual studio knows what is "NewBitmap" in the AdjustBrightness method, but it doesn't know what is "NewBitmap" in the trackBar1_Scroll
private void trackBar1_Scroll(object sender, EventArgs e)
{
lblBrightNum.Text = trackBar1.Value.ToString();
//Visual studio is complaining about this, you haven't define "NewBitmap", you can fix by adding below:
Bitmap NewBitmap = //your bitmap
picBox.Image = AdjustBrightness(NewBitmap, trackBar1.Value);
}
public static Bitmap AdjustBrightness(Bitmap Image, int Value)
{
Bitmap TempBitmap = (Bitmap)Image.Clone();
float FinalValue = (float)Value / 255.0f;
Bitmap NewBitmap = new Bitmap(TempBitmap.Width, TempBitmap.Height);
Graphics NewGraphics = Graphics.FromImage(NewBitmap);
float[][] FloatColorMatrix ={
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[] {FinalValue, FinalValue, FinalValue, 1, 1}
};
ColorMatrix NewColorMatrix = new ColorMatrix(FloatColorMatrix);
ImageAttributes Attributes = new ImageAttributes();
Attributes.SetColorMatrix(NewColorMatrix);
NewGraphics.DrawImage(TempBitmap, new Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, GraphicsUnit.Pixel, Attributes);
Attributes.Dispose();
NewGraphics.Dispose();
return NewBitmap;

Related

ColorMatrix does not apply

What am I trying to do?
The following function should simply return the image defined by parameter "sourceImage" with changed "opacity":
public static Image DisableImage(Image sourceImage)
{
Image newImage = new Bitmap(sourceImage);
using (Graphics g = Graphics.FromImage(newImage))
{
using (ImageAttributes imageAttributes = new ImageAttributes())
{
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.Matrix33 = 0.5f;
imageAttributes.SetColorMatrix(colorMatrix);
//Draw the original image on the new image using the color matrix
g.DrawImage(sourceImage, new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), 0, 0, sourceImage.Width, sourceImage.Height, GraphicsUnit.Pixel, imageAttributes);
}
}
return newImage;
}
What is the problem?
The returned image seems to be not changed at all.
What have I tried?
If changing the following line
ColorMatrix colorMatrix = new ColorMatrix();
to
ColorMatrix colorMatrix = new ColorMatrix(
new float[][]{
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {Color.Red.R / 255.0f,
Color.Red.G / 255.0f,
Color.Red.B / 255.0f,
0, 1}
});
a red half-transparent "film" is drawn on the image, which makes me suspect that I missed something in how to use the ColorMatrix.
Any help is appreciated, thanks!
Alright, I figured it out myself.
Changing this line:
Image newImage = new Bitmap(sourceImage);
to
Image newImage = new Bitmap(sourceImage.Width, sourceImage.Height);
was the solution.
I was probably drawing on top of the existing image, which was of course not what I wanted.
Any possibility to delete this question?

Change image color with transparent background

I need to load an image with green circle over a transparent background into a bitmap image using c# (System.Drawings).
That's the easy part. However I need to change the color of the circle before adding it to the bigger image, without affecting the transparency of the surrounding. In my case I need to change the circle color to yellow and add it as a sun.
I can't use fixed yellow circle image because the desired color is dynamic.
So in the code below, how can I change the color of the image before adding it to the bitmap?
Image i = Image.FromFile(greenCircleFile);
Bitmap b = new Bitmap(500, 500);
using(Graphics g = Graphics.FromImage(b))
{
//--> Here I need to change the color of the green circle to yellow
//afterwards I can add it to the bitmap image
g.DrawImage(i, 0, 0, 500, 500);
}
Please note that two things need to be into consideration: Keeping the anti-aliasing of the shape (circle), and the color needs to be picked by user and used as is to overlay the original color of the circle.
Fixed:
Thanks to #TaW, he provided the correct answer. However with a glitch, here's the final version that worked for me:
Image i = Image.FromFile(greenCircleFile);
Bitmap b = new Bitmap(500, 500);
using(Graphics g = Graphics.FromImage(b))
{
//Here I need to change the color of the green circle to yellow
i = ChangeToColor(b, Color.Gold)
//afterwards I can add it to the bitmap image
g.DrawImage(i, 0, 0, 500, 500);
}
While ChangeToColor function is as follows:
Bitmap ChangeToColor(Bitmap bmp, Color c)
{
Bitmap bmp2 = new Bitmap(bmp.Width, bmp.Height);
using (Graphics g = Graphics.FromImage(bmp2))
{
float tr = c.R / 255f;
float tg = c.G / 255f;
float tb = c.B / 255f;
ColorMatrix colorMatrix = new ColorMatrix(new float[][]
{
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {tr, tg, tb, 0, 1}
});
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height),
0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attributes);
}
return bmp2;
}
This will create a new Bitmap with all non-transparent pixels moved strongly toward a new color:
Bitmap ChangeToColor(Bitmap bmp, Color c)
{
Bitmap bmp2 = new Bitmap(bmp.Width, bmp.Height);
using (Graphics g = Graphics.FromImage(bmp2))
{
float tr = c.R / 255f;
float tg = c.G / 255f;
float tb = c.B / 255f;
ColorMatrix colorMatrix = new ColorMatrix(new float[][]
{
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {tr, tg, tb, 0, 1} // kudos to OP!
});
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height),
0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attributes);
}
return bmp2;
}
do make sure not to leak the Bitmaps you create!
Note that there are other methods as well. Here is a link to a method that uses ColorMapping. This allows for a range of colors to be replaced by another range, so it can keep gradients like the ones you get in anti-alised graphics..
Here's my solution you just need to create a new Control
then inherit the Picturebox check this out.
public partial class UICirclePicture : PictureBox
{
[Browsable(false)]
public int Depth { get; set; }
[Browsable(false)]
public SprikiwikiUI Ui
{
get { return SprikiwikiUI.Instance; }
}
[Browsable(false)]
public MouseState MouseState { get; set; }
public UICirclePicture()
{
BackColor = Ui.GetApplicationBackgroundColor();
SizeMode = PictureBoxSizeMode.StretchImage;
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
using (var gp = new GraphicsPath())
{
gp.AddEllipse(new Rectangle(0, 0, this.Width - 1, this.Height - 1));
this.Region = new Region(gp);
}
}
}

DrawImage c# How to change colors to black-white into the top left triangle?

Here is my code:
public static void ReduceScreenshot(string fileName)
{
var bmpSS = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
var gfxSS = Graphics.FromImage(bmpSS);
gfxSS.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
ColorMatrix colorMatrix = new ColorMatrix(
new float[][]
{
new float[] { 1.5f, 1.5f, 1.5f, 0, 0},
new float[] { 1.5f,1.5f, 1.5f, 0, 0},
new float[] {1.5f, 1.5f, 1.5f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {-1, -1, -1, 0, 1}
});
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
Rectangle abc= new Rectangle(-783, -383, bmpSS.Width, bmpSS.Height);
gfxSS.DrawImage(bmpSS, abc, -783, -383, bmpSS.Width, bmpSS.Height, GraphicsUnit.Pixel, attributes);
bmpSS.Save("ScreenshotGray.png", ImageFormat.Png);
}
}
It's how it work http://take.ms/6tzvU. How should i change area from rectangle to triangle?
Not sure just which points shall set up the triangle but the code to restrict DrawImage to it should look something like this:
using System.Drawing.Imaging;
..
..
Rectangle abc = new Rectangle(-783, -383, bmpSS.Width, bmpSS.Height);
GraphicsPath gp = new GraphicsPath();
// three points to make up a triangle
// repeat the first one as one way of closing the path
// use your own coordinates!
Point[] p = new Point[] { new Point(12, 34), new Point(56, 78),
new Point(90, 12), new Point(12, 34) };
gp.AddLines(p); // or AddPolygon
gfxSS.SetClip(gp); // now restrict the Graphics object to the interior of the path
gfxSS.DrawImage(bmpSS, abc, -783, -383, bmpSS.Width, bmpSS.Height,
GraphicsUnit.Pixel, attributes);

Preventing effects from over laying?

So basically I've got 2 effects. 1 is Sepia and the other is some Aqua effect, when I put the Aqua effect onto the image and then go to put the Sepia effect onto it the Sepia effect will overlay the Aqua. I don't understand why it is doing it, I know to add an additional picture box to it and make the picturebox invisible but this isn't an efficient way of doing it, their must be another more efficient way of doing it, can anyone please share?
Sepia
private void btnGrayscale_Click(object sender, EventArgs e)
{
Bitmap grayScale = (Bitmap)picOriginal.Image.Clone();
int height = grayScale.Size.Height;
int width = grayScale.Size.Width;
for (int yCoordinate = 0; yCoordinate < height; yCoordinate++)
{
for (int xCoordinate = 0; xCoordinate < width; xCoordinate++)
{
Color color = grayScale.GetPixel(xCoordinate, yCoordinate);
int grayColor = (color.R + color.G + color.B) / 3;
grayScale.SetPixel(xCoordinate, yCoordinate, Color.FromArgb(grayColor,grayColor,grayColor));
}
}
picModified.Image = grayScale;
}
Aqua
ColorMatrix matrix = new ColorMatrix(new float[][]{
new float[] {0, 4, 0, 0, 0},
new float[] {0, 0, 4, 0, 4},
new float[] {0, 0, 0, 4, 0},
new float[] { 0, 0, 4, 0, 1},
new float[] { 0, 0, 0, 4, 0}
});
Image image = (Bitmap)picOriginal.Image.Clone();
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(matrix);
Graphics graphics = Graphics.FromImage(image);
graphics.DrawImage(image,
new Rectangle(0, 0, image.Width, image.Height),0,0,image.Width,image.Height,
GraphicsUnit.Pixel,attributes);
graphics.Dispose();
picModified.Image = image;
}

how to greyout an image in asp.net

I need to grey out disabled images in the asp page but I dont know how to achieve that
my current code is given below
<ul class="flag">
<li>
<img src="flag-china.png" alt="china" onclick="setLanguage('Chinese');" style="cursor:pointer"/></li>
<li>
<img src="flag-france.png" alt="france" onclick="setLanguage('French');" style="cursor:pointer" /></li>
<img src="us.png" alt="us" onclick="setLanguage('English');" style="cursor:pointer" /></li>
</ul>
I need to disable and grey out every flag except the last one
Found following code sample here. Call the gray out version when you need.
public static Bitmap MakeGrayscale3(Bitmap original)
{
//create a blank bitmap the same size as original
Bitmap newBitmap = new Bitmap(original.Width, original.Height);
//get a graphics object from the new image
Graphics g = Graphics.FromImage(newBitmap);
//create the grayscale ColorMatrix
ColorMatrix colorMatrix = new ColorMatrix(
new float[][]
{
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
//create some image attributes
ImageAttributes attributes = new ImageAttributes();
//set the color matrix attribute
attributes.SetColorMatrix(colorMatrix);
//draw the original image on the new image
//using the grayscale color matrix
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
//dispose the Graphics object
g.Dispose();
return newBitmap;
}

Categories

Resources