Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to add text watermark to multiple images from a directory and save it to a directory. Here is what I have so far:
using System.Web.Mvc;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace something.Controllers
{

    public class something: Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(string text, string text1)
        {
            DirectoryInfo dir = new DirectoryInfo(@"C:\Users\name\Desktop\Images");
            FileInfo[] files = dir.GetFiles();
            string value = text;
            string value1 = text1;
            foreach (FileInfo file in files)
            {
          

                using (Bitmap bitmap = new Bitmap( file.FullName))
                {
                    using (Graphics graphics = Graphics.FromImage(bitmap))
                    {
                        Brush brush = new SolidBrush(Color.Red);
                        Font font = new Font("Arial", 10, FontStyle.Italic, 
                        GraphicsUnit.Pixel);
                        SizeF textSize = new SizeF();
                        textSize = graphics.MeasureString(value, font);
                        Point position = new Point(bitmap.Width - ((int)textSize.Width + 
                        30), bitmap.Height - ((int)textSize.Height + 10));
                        graphics.DrawString((value + value1), font, brush, position);
                        using (MemoryStream mStream = new MemoryStream())
                        {
                            mStream.Position = 0;
                            bitmap.Save(mStream, ImageFormat.Png);
                           


                            string _path = 
                            Path.Combine(Server.MapPath("~/UploadedFolders"), 
                            file.Name);
                            bitmap.Save(_path);
                            return File(mStream.ToArray(), "image/png", file.Name);
                        }
                    }
                }



            }
            return View();
        }

    }

}

Here is my view :
@model something.something

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>UploadFile</h2>

@{
    ViewBag.Title = "UploadFile";
}


@using (Html.BeginForm("Index", "something", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()



    <div class="form-group">
        @Html.LabelFor(m => m.preFix)
        
        <input type="text" class="form-control" required name="text" id="preFix" />
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.pageNumber)

        <input type="text" class="form-control" required name="text1" id="pageNumber" />
    </div>
 


    <input type="submit" class="btn btn-info" value="Upload" />

    @ViewBag.Message




}

With this code it goes to the path and only add a water mark to the first image in the folder and save it.
Thank you

What I have tried:

Bitmap picture = new Bitmap(@"C:\Users\Desktop\A\" + file.FullName + ".bmp");
Posted
Updated 3-Jan-20 7:07am
v2

1 solution

The return
C#
return File(mStream.ToArray(), "image/png", file.Name);

inside the loop prevents the loop from looping.
A return ends the execution of current piece of code.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v2
Comments
AskalotLearnalot 3-Jan-20 13:33pm    
I don't know if I should be happy that everything work or sad because I missed something that simple. thank you.

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