我无法使用此代码加水印,有人可以帮我解决代码中的错误吗 [英] im not able to watermark using this code can someone pls help me out with wats wrong in the code

查看:85
本文介绍了我无法使用此代码加水印,有人可以帮我解决代码中的错误吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void btnPreview_Click(object sender, EventArgs e)
        {
             // Get a graphics context
            float opac = 1;
            string sOpacity = cboOpacity.Text;
            // Determine the opacity of the watermark
            switch (sOpacity)
            {
                case "100%":
                    opac = 0.25f; // 1 * 255
                    break;
                case "75%":
                    opac = 0.75f; // .75 * 255
                    break;
                case "50%":
                    opac = 0.50f; // .5 * 255
                    break;
                case "25%":
                    opac = 0.25f;  // .25 * 255
                    break;
                case "10%":
                    opac = 0.10f;  // .10 * 255
                    break;
                default:
                    opac = 0.50f; // default at 50%; .5 * 255
                    break;
            }

           
            // Set the drawing position based on the users
            // selection of placing the text at the positions
            // specified below
            String position = cboPositions.Text;
            logo = Image.FromFile(openFileDialog1.FileName, true);
            switch (position)
            {
                case "TopLeft":
                    x = 0; y = 0;
                    break;
                case "TopRight":
                    x = (int)(picContainer.Image.Width - logo.Width); y = 0;
                    break;
                case "TopMiddle":
                    x = (int)(picContainer.Image.Width - logo.Width) / 2; y = 0;
                    break;
                case "BottomLeft":
                    x = 0; y = (int)(picContainer.Image.Height - logo.Height);
                    break;
                case "BottomRight":
                    x = (int)(picContainer.Image.Width - logo.Width); y = (int)(picContainer.Image.Height - logo.Height);
                    break;
                case "BottomMiddle":
                    x = (int)(picContainer.Image.Width - logo.Width) / 2; y = (int)(picContainer.Image.Height - logo.Height);
                    break;
                case "MiddleLeft":
                    x = 0; y = (int)(picContainer.Image.Height - logo.Height) / 2;
                    break;
                case "MiddleRight":
                    x = (int)(picContainer.Image.Width - logo.Width); y = (int)(picContainer.Image.Height - logo.Height) / 2;
                    break;
                case "Center":
                    x = (int)(picContainer.Image.Width - logo.Width) / 2; y = (int)(picContainer.Image.Height - logo.Height) / 2;
                    break;
                default:
                    break;
            }
            Graphics g = Graphics.FromImage(img);
            logo = Image.FromFile(openFileDialog1.FileName, true);
            Graphics g1 = Graphics.FromImage(logo);       
            Rectangle destRect=new Rectangle();
            ColorMatrix colorMatrix = new ColorMatrix(
                new float[][] { 
                    new float[] { 1, 0f, 0f, 0f, 0f},
                    new float[] { 0f, 1, 0f, 0f, 0f},
                    new float[] { 0f, 0f, 1, 0f, 0f},
                    new float[] { 0f, 0f, 0f, opac, 0f},
                    new float[] { 0f, 0f, 0f, 0f, 1}                    
                });
            ImageAttributes attributes=new ImageAttributes();
            using (Graphics ga = Graphics.FromImage(logo))
            {
                ga.DrawImage(logo, destRect, 0, 0, logo.Width, logo.Height, GraphicsUnit.Pixel, attributes);
            }
            //g = Graphics.FromImage(img);
            //picContainer.Image =img;
        }


ga.DrawImage使用此即时贴尝试将图片嵌入到另一张图片上,但无法执行此操作,这可能会帮助我解决这个问题.


ga.DrawImage using this im trying to embed a picture onto another picture but im not able to do it can some one pls help me out wit this.

推荐答案

将要发布代码,然后确保其效果尽如人意.这意味着:
1)不要在其中留下旧的和错误的注释,以使问题变得混乱:
If you are going to post code, then make sure it is as good as you can make it. This means:
1) Not leaving old - and wrong - comments in there to confuse the issue:
 // Get a graphics context
float opac = 1;

2)不要生成虚假的和未使用的Graphics对象,并始终处理创建的对象:

2) Don''t generate spurious - and unused - Graphics objects, and always Dispose the ones you do create:

Graphics g = Graphics.FromImage(img);
logo = Image.FromFile(openFileDialog1.FileName, true);
Graphics g1 = Graphics.FromImage(logo);
Rectangle destRect=new Rectangle();


3)切掉不相关的东西.这意味着一切


3) Cut out the irrelevant stuff. That means everything from

 // Get a graphics context
float opac = 1;
string sOpacity = cboOpacity.Text;


Graphics g = Graphics.FromImage(img);


ColorMatrix colorMatrix = new ColorMatrix(
    new float[][] {
        new float[] { 1, 0f, 0f, 0f, 0f},
        new float[] { 0f, 1, 0f, 0f, 0f},
        new float[] { 0f, 0f, 1, 0f, 0f},
        new float[] { 0f, 0f, 0f, opac, 0f},
        new float[] { 0f, 0f, 0f, 0f, 1}
    });

含.摆脱掉底部的注释.
这使您的例行工作为:

Inclusive. Get rid of the comments at the bottom too.
That leaves your routine as:

private void btnPreview_Click(object sender, EventArgs e)
        {
            logo = Image.FromFile(openFileDialog1.FileName, true);
            Rectangle destRect=new Rectangle();
            ImageAttributes attributes=new ImageAttributes();
            using (Graphics ga = Graphics.FromImage(logo))
            {
                ga.DrawImage(logo, destRect, 0, 0, logo.Width, logo.Height, GraphicsUnit.Pixel, attributes);
            }
        }

然后更好地解释您的问题:我做不到"并不是全部有帮助.

幸运的是,您只需要该片段(只需要一点点和鲍勃就可以使编译器满意).而所有您需要改变.因为现在,您甚至可以看到问题所在:正在尝试在其中绘制水印的矩形有多大?

Then explain your problem better: "I''m not able to do it" isn''t all that helpful.

Fortunately, that fragment is all you need (baring some bits and bobs to keep the compiler happy). And all you need to change. Because now, even you can see what the problem is: How big is the rectangle you are trying to draw your watermark in?


这篇关于我无法使用此代码加水印,有人可以帮我解决代码中的错误吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆