最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Add date on image while uploading in c# - Stack Overflow

programmeradmin3浏览0评论

I am adding date and image to the database. I want to add the date as the footer to the uploaded image.

HTML for image upload

<div class="form-group">
    @Html.Label("Photo", htmlAttributes: new { @class = "control-label" })
    <div class="col-md-10">
        <img id="DocImg" src="~/Upload/DoctorImage/doc-default.png" style="cursor: pointer;"  accesskeyclass="edtImg" width="100" height="100" />
        <input type="file" id="fileUpload" name="Photo" accept="image/*" />
    </div>
</div>

HTML for datepicker

 <div class="col-6">
    @Html.ValidationSummary(true, "", new { @class = "text-danger"})
    <div class="form-group">
        @Html.Label("Joining Date", htmlAttributes: new { @class = "control-label col-md-2", @required = "required" })
        <div class="col-md-10">
            @(Html.Kendo().DatePicker()
                            .Name("JoiningDate")
                            .Value("")
                            .HtmlAttributes(new { style = "width: 100%", required = "true" })
            )
            @Html.ValidationMessageFor(model => model.JoiningDate, "", new { @class = "text-danger" })
        </div>
    </div>
</div>

Script for Upload

$(document).ready(function () {
    $("#fileUpload").hide();
});

$("#DocImg").click(function () {
    $("#fileUpload").trigger('click');
});

I am adding date and image to the database. I want to add the date as the footer to the uploaded image.

HTML for image upload

<div class="form-group">
    @Html.Label("Photo", htmlAttributes: new { @class = "control-label" })
    <div class="col-md-10">
        <img id="DocImg" src="~/Upload/DoctorImage/doc-default.png" style="cursor: pointer;"  accesskeyclass="edtImg" width="100" height="100" />
        <input type="file" id="fileUpload" name="Photo" accept="image/*" />
    </div>
</div>

HTML for datepicker

 <div class="col-6">
    @Html.ValidationSummary(true, "", new { @class = "text-danger"})
    <div class="form-group">
        @Html.Label("Joining Date", htmlAttributes: new { @class = "control-label col-md-2", @required = "required" })
        <div class="col-md-10">
            @(Html.Kendo().DatePicker()
                            .Name("JoiningDate")
                            .Value("")
                            .HtmlAttributes(new { style = "width: 100%", required = "true" })
            )
            @Html.ValidationMessageFor(model => model.JoiningDate, "", new { @class = "text-danger" })
        </div>
    </div>
</div>

Script for Upload

$(document).ready(function () {
    $("#fileUpload").hide();
});

$("#DocImg").click(function () {
    $("#fileUpload").trigger('click');
});
Share Improve this question edited Sep 21, 2017 at 5:31 Alsamil Mehboob asked Aug 10, 2017 at 11:06 Alsamil MehboobAlsamil Mehboob 4042 gold badges7 silver badges24 bronze badges 6
  • 1 What does date as the footer mean? – mplungjan Commented Aug 10, 2017 at 11:10
  • found this in a thread. stackoverflow./questions/1224653/… does this help? – Md. Tazbir Ur Rahman Bhuiyan Commented Aug 10, 2017 at 11:13
  • It would make more sense to set any file metadata on the server side - especially the date. – Rory McCrossan Commented Aug 10, 2017 at 11:18
  • 1 hai mplungjan, he mean when i am uploaded image already edited and prited date of bottom of that image. which not outside the image he want inside the image – Abdulla Sirajudeen Commented Aug 11, 2017 at 5:34
  • 1 I think you mean something like this – Munzer Commented Aug 24, 2017 at 5:53
 |  Show 1 more ment

2 Answers 2

Reset to default 9 +50

I think you need to manipulate the image in the DOM before the post pletes, using ajax to acplish it

Having this in mind, all you need is to use a canvas to render the text from the datepicker inside the image.

As shown in the code -

var canvasEl = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

canvasEl.width = $('img').width();
canvasEl.crossOrigin = "Anonymous";
canvasEl.height = $('img').height();
ctx.drawImage($('img').get(0), 0, 0);
ctx.font = "36pt Verdana";

$(document).on('input','#inp',function(){
    //redraw image
    ctx.clearRect(0,0,canvasEl.width,canvasEl.height);
    ctx.drawImage($('img').get(0), 0, 0);
    
    //refill text
    ctx.fillStyle = "red";
    ctx.fillText($(this).val(),40,80); //positioning text wherever you want
});

$('button').click(function(){
    console.log(ctx.getImageData(50, 50, 100, 100));
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
    <img style="display:none" src="https://encrypted-tbn2.gstatic./images?q=tbn:ANd9GcTsEbEB4kEMHt3WJ13HpZE16hJ3iKrE8ugnErvyln7oNPDma48U" crossorigin="anonymous"/>
 <input type="text" id="inp"/>
  <button type="submit">Save</button>
</form>

<canvas id="canvas" />

Please share if it works for you?

You gonna need a reference to System.Drawing and play a little bit with graphics. I just authored a solution for you so it isn't bulletproof. You might wanna play around to change the colors, font, etc. as well as add exception handling.

public void AddWatermark(string inputhPath, string outputPath)
{
    var wm = DateTime.Now.ToShortDateString();

    using (var bmp = Bitmap.FromFile(inputhPath))
    using (var g = Graphics.FromImage(bmp))
    using (var transparentBrush = new SolidBrush(Color.FromArgb(164, 0, 0, 0)))
    using (var font = new Font("Calibri", 20, FontStyle.Bold, GraphicsUnit.Pixel))
    {
        var format = new StringFormat(StringFormatFlags.NoWrap);
        var dim = g.MeasureString(wm, font);
        var loc = new Point(bmp.Width - (int)dim.Width - 20, bmp.Height - (int)dim.Height * 2);

        g.DrawString(wm, font, transparentBrush, loc);

        bmp.Save(outputPath);
    }
}
发布评论

评论列表(0)

  1. 暂无评论