0

I am having a unity project in which I have made a c# scripting to change the brightness of an image.

As using System.Drawing is not supported in unity, I imported a System.Drawing.dll file for that.

So I was able to use that.

Here is my code below.

using UnityEngine;
using System.Collections;
using System;
using System.Drawing;
using System.Drawing.Imaging;


public class NewBehaviourScript : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () 
    {
        Bitmap originalImage;
        Bitmap adjustedImage;
        float brightness = 1.0f; // no change in brightness
        float contrast = 2.0f; // twice the contrast
        float gamma = 1.0f; // no change in gamma

        float adjustedBrightness = brightness - 1.0f;
        // create matrix that will brighten and contrast the image
        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 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,originalImage.Width,originalImage.Height,
                    GraphicsUnit.Pixel, imageAttributes);
    }
}

With the error above, I tried it to solve using this but it still doesn't solve my problem.

How could I solve this problem?

Community
  • 1
  • 1
Manthan
  • 3,856
  • 1
  • 27
  • 58
  • You can either remove `System.Drawing` from your usings or use specifically `UnityEngine.Graphics` – Stephan Bauer May 05 '15 at 06:16
  • If I don't use System.Drawing, it doesn't let me use Bitmap and all other as they are referenced to that framework... – Manthan May 05 '15 at 06:23

1 Answers1

3

Why don't you use a fully qualified name instead (which includes the namespace) such as

System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(...

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • Thanks for your reply but it doesn't show me FromImage. Can you please update in my code? Because without using System.Drawing, it gives me error on Bitmap... What should I do? – Manthan May 05 '15 at 06:22
  • When I type in Graphics g = Graphics.FromImage, it doesn't show me fromImage. If you have unity can you please run the above code and check? – Manthan May 06 '15 at 04:27
  • @Manthan `Graphics.FromImage` is in `System.Drawing`'s `Graphics` class. – Aniket Inge May 06 '15 at 05:02
  • Yes, I know and that is why I have used that namespace above in my code but still it doesn't let me take that fromImage. shows error. – Manthan May 06 '15 at 05:36
  • post screenshot of the error and relevant parts of your sourcecode – Aniket Inge May 06 '15 at 05:49
  • The code is same as above.... This is the actual file only... This is the code only... – Manthan May 06 '15 at 05:51
  • @Manthan does the code still look like `Graphics g = Graphics.FromImage(adjustedImage);` – Aniket Inge May 06 '15 at 05:59
  • Can you please run the code in your system and check and give me the correct code? Please if possible. Sorry for the trouble.... – Manthan May 06 '15 at 06:04
  • As I am not aware with c# too much. I am in ios and making a unity project with c# scripting. – Manthan May 06 '15 at 06:07
  • I don't have Unity3d. I will tell you the problem anyway. See there is a `Graphics` class in `UnityEngine` namespace. And there is a `Graphics` class in `System.Drawing` namespace. Because you are `using` both `UnityEngine` as well as `System.Drawing` namespace in your code, the compiler is confused as to which `Graphics` class you are referring to. Hence to solve this problem you have to change the source code to look like: `System.Drawing.Graphics g= new System.Drawing.Graphics.FromImage(adjustedImage);` – Aniket Inge May 06 '15 at 06:07