 |
|
 |
Wonderful. I wanna use it on my website. Thanks.
|
|
|
|
 |
|
 |
Great article. Thanks so much. You saved my day
|
|
|
|
 |
|
 |
I modified slightly to make a class and added it to my App_Code. I didn't use the Copyright, only because I didn't need it.
From the codebehind I do this
BasePath = "~/aFileInMySolution/";
PhotoFilename = "origImage.jpg";
WatermarkFilename = "anImageLogo.png";
Copyright = "Copyright 2011";
WatermarkMyImage myImage = new WatermarkMyImage();
string newFileName = myImage.AddWatermarkLayer(Server.MapPath(BasePath), PhotoFilename, WatermarkFilename, Copyright);
backgroundPath = System.Web.VirtualPathUtility.ToAbsolute(BasePath + newFileName);
Here's the method in my WatermarkMyImage class:
public string AddWatermarkLayer(string basePath, string photoFilename, string watermarkFilename, string copyrightString)
{
//Create the image object from the path
System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(basePath + photoFilename);
//Get the dimensions of imgPhoto
int phWidth = imgPhoto.Width;
int phHeight = imgPhoto.Height;
//Create a new object from the imgPhoto
Bitmap bmPhoto = new Bitmap(phWidth, phHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(72, 72);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
//Load the watermark image saved as .bmp and set with background color of green (Alpha=0, R=106, G=125, B=106)
System.Drawing.Image imgWatermark = System.Drawing.Image.FromFile(basePath + watermarkFilename);
//Size of imgWatermark
int wmWidth = imgWatermark.Width;
int wmHeight = imgWatermark.Height;
//Draws the imgPhoto to the graphics object position at (x-0, y=0) 100% of original
grPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, phWidth, phHeight), 0, 0, phWidth, phHeight, GraphicsUnit.Pixel);
//To maximize the size of the Logo text using seven different fonts
int[] sizes = new int[] { 16, 14, 12, 10, 8, 6, 4 };
Font crFont = null;
SizeF crSize = new SizeF();
//Determines the largest possible size of the font
for (int i = 0; i < 7; i++)
{
crFont = new Font("arial", sizes[i], FontStyle.Bold);
crSize = grPhoto.MeasureString(copyrightString, crFont);
if ((ushort)crSize.Width < (ushort)phWidth)
break;
}
//For photos with varying heights, determines a five percent position from bottom of image
int yPixlesFromBottom = (int)(phHeight * .05);
float yPosFromBottom = (int)((phHeight - yPixlesFromBottom) - (crSize.Height / 2));
float xCenterOfImg = (phWidth / 2);
StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Center;
//Create a brush with 60% Black (Alpha 153)
SolidBrush semiTransparBrushOne = new SolidBrush(Color.FromArgb(153, 0, 0, 0));
//Creates a shadow effect
grPhoto.DrawString(copyrightString, crFont, semiTransparBrushOne, new PointF(xCenterOfImg + 1, yPosFromBottom + 1), StrFormat);
//Create a brush with 60% White (Alpha 153)
SolidBrush semiTransparBrushTwo = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
//Draws the same text on top of the previous
grPhoto.DrawString(copyrightString, crFont, semiTransparBrushTwo, new PointF(xCenterOfImg + 1, yPosFromBottom + 1), StrFormat);
//Set the above into a bitmap
Bitmap bmWatermark = new Bitmap(bmPhoto);
bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
Graphics grWatermark = Graphics.FromImage(bmWatermark);
//Apply two color manipulations for the watermark
ImageAttributes imageAttributes = new ImageAttributes();
ColorMap colorMap = new ColorMap();
colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
System.Drawing.Imaging.ColorMap[] remapTable = { colorMap };
imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
//Change the opacity of watermark by setting 3rd row, 3rd col to .3f
float[][] colorMatrixElements = {
new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.3f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};
ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
//Draw the watermark in the upper right hand corner of photo
int xPosOfWm = ((phWidth - wmWidth) - 10);
int yPosOfWm = 10;
grWatermark.DrawImage(imgWatermark, new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight), 0, 0, wmWidth, wmHeight, GraphicsUnit.Pixel, imageAttributes);
//Finally, replace the original image with new
imgPhoto = bmWatermark;
grPhoto.Dispose();
grWatermark.Dispose();
//Save as original name but concatenate _watermarked to it
string newFilename = "watermarked_" + photoFilename;
imgPhoto.Save(basePath + newFilename, ImageFormat.Jpeg);
imgWatermark.Dispose();
imgPhoto.Dispose();
return newFilename;
}
Thank you so much!
|
|
|
|
 |
|
|
 |
|
 |
Greetings,
I am currently working on a watermarking application. I am considering a perceptual and non-perceptual watermarks. The thing I am hesitating on is the choice of programming language. Is C# good for such application in general? I am not very familiar whether I can do low-level programming with C#, as compared with C++? What would you suggest?
Regards!
|
|
|
|
 |
|
 |
http://www.codeproject.com/KB/GDI-plus/watermark.aspx/?fid=8753&df=90&mpp=25&sort=Position&tid=3611895
i used the watermark example given on the page,,. i working but image i uploaded is getting blurred,. can i avoid it,.
|
|
|
|
 |
|
 |
How can I apply watermark settings to a printer, so that each page will print with that watermark?
Please help
|
|
|
|
 |
|
 |
Simple and direct! Great!
|
|
|
|
 |
|
 |
The watermark text flows from left buttom corner to right top corner?
DesignerInCore
|
|
|
|
 |
|
|
 |
|
 |
Brilliant piece of code, i am using this for watermaring, but without the text feature, that is in my watermark image
|
|
|
|
 |
|
 |
HI friends ,
CAN I SET IMAGE OVER VEDIO AT CENTER.......
i AM UNABLE TO DO THIS
PLZ HELP ME
MY CODE FOR COPY LOGO.......
// HERE N IS VALUE OR LINE NUMBER BUT INSTEAD OF THIS I HAVE TO USE X, Y CORDINATES FOR SET LOGO IN CENTER........
lock (this)
{
if (m_bmdLogo != null)
{
int N = 15;
IntPtr ipSource = m_bmdLogo.Scan0;
IntPtr ipDest = (IntPtr)(pBuffer.ToInt32() + (320 * 4) * N);
for (int x = 0; x < m_bmdLogo.Height; x++)
{
CopyMemory(ipDest, ipSource, ((uint)m_bmdLogo.Stride));
// Marshal.Copy(ipDest, savedArray, 0, ipSource);
ipDest = ((IntPtr)(ipDest.ToInt32() + stride));
//BufferLen=
ipSource = ((IntPtr)(ipSource.ToInt32() + m_bmdLogo.Stride));
//ipSource = ((IntPtr)(ipSource.ToInt32() + BufferLen));
}
}
}
|
|
|
|
 |
|
 |
hi friends,
I am working an desktop application IN C# .NET in which I want use water marking functionality... I have done this but there is a problem in showing text or image as water marking in currect way. whatever i want show as water marking in my vedio it is coming in after vertical flip .
***********<u>I want to show image or text AS IT IS NOT FLIPED </u>*********
CODE which I am using.................PLZ HELP WHERE IT IS CREATING PROBLEM
/****************************************************************************
While the underlying libraries are covered by LGPL, this sample is released
as public domain. It is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.
*****************************************************************************/
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Diagnostics;
using DirectShowLib;
namespace DxLogo
{
/// <summary> Summary description for MainForm. </summary>
///
internal class Capture : ISampleGrabberCB, IDisposable
//internal class Capture : IDisposable
{
#region Member variables
/// <summary> graph builder interface. </summary>
private IFilterGraph2 m_FilterGraph = null;
IMediaControl m_mediaCtrl = null;
/// <summary> Set by async routine when it captures an image </summary>
private bool m_bRunning = false;
/// <summary> Dimensions of the image, calculated once in constructor. </summary>
private int m_videoWidth;
private int m_videoHeight;
private int m_stride;
public static string FileName;
BitmapData m_bmdLogo = null;
Bitmap m_Bitmap = null;
#if DEBUG
// Allow you to "Connect to remote graph" from GraphEdit
DsROTEntry m_rot = null;
#endif
#endregion
#region API
[DllImport("Kernel32.dll", EntryPoint = "RtlMoveMemory")]
private static extern void CopyMemory(IntPtr Destination, IntPtr Source, [MarshalAs(UnmanagedType.U4)] uint Length);
#endregion
/// zero based device index, and some device parms, plus the file name to save to
public Capture(int iDeviceNum, int iFrameRate, int iWidth, int iHeight, string FileName)
{
DsDevice[] capDevices;
// Get the collection of video devices
capDevices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
if (iDeviceNum + 1 > capDevices.Length)
{
throw new Exception("No video capture devices found at that index!");
}
try
{
// Set up the capture graph
SetupGraph( capDevices[iDeviceNum], iFrameRate, iWidth, iHeight, FileName);
}
catch
{
Dispose();
throw;
}
}
/// <summary> release everything. </summary>
public void Dispose()
{
CloseInterfaces();
if (m_Bitmap != null)
{
m_Bitmap.UnlockBits(m_bmdLogo);
m_Bitmap = null;
m_bmdLogo = null;
}
}
// Destructor
~Capture()
{
CloseInterfaces();
}
/// <summary> capture the next image </summary>
public void Start()
{
if (!m_bRunning)
{
int hr = m_mediaCtrl.Run();
DsError.ThrowExceptionForHR( hr );
m_bRunning = true;
}
}
// Pause the capture graph.
// Running the graph takes up a lot of resources. Pause it when it
// isn't needed.
public void Pause()
{
if (m_bRunning)
{
int hr = m_mediaCtrl.Pause();
DsError.ThrowExceptionForHR( hr );
m_bRunning = false;
}
}
/// <summary> Specify the logo file to write onto each frame </summary>
public void SetLogo(string fileName)
{
//public Bitmap WaterMarkToImage(string fileName, string Filenameasp)
////{
// Bitmap bmp;
//bmp = new Bitmap(fileName);
// Graphics graphicsObject;
// int x, y;
//try
//{ //create graphics object from bitmap
//graphicsObject = Graphics.FromImage(bmp);
//}
// catch (Exception e)
//{
//Bitmap bmpNew = new Bitmap(bmp.Width, bmp.Height);
// graphicsObject = Graphics.FromImage(bmpNew);
// graphicsObject.DrawImage(bmp, new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel);
// bmp = bmpNew;
//}
// int startsize = (bmp.Width / Filenameasp.Length);
//get the font size with respect to length of the string
//x and y cordinates to draw a string
//x = 0;
//y = bmp.Height / 2;
// y = 0;
// System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.DirectionVertical); -> draws a vertical string for watermark
//System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.NoWrap);
//drawing string on Image
//graphicsObject.DrawString(Filenameasp, new Font("Verdana", startsize, FontStyle.Bold), new SolidBrush(Color.FromArgb(60, 255, 255, 255)), x, y, drawFormat);
//return a water marked image
//return (bmp);
// }
lock (this)
{
if (fileName.Length > 0)
{
Bitmap m_Bitmap = new Bitmap(fileName);
Rectangle r = new Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height);
//m_Bitmap.LockBits(r, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
m_bmdLogo = m_Bitmap.LockBits(r, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
// m_Bitmap.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
}
else
{
if (m_Bitmap != null)
{
m_Bitmap.UnlockBits(m_bmdLogo);
m_Bitmap = null;
m_bmdLogo = null;
}
}
}
}
/// <summary> build the capture graph for grabber. </summary>
private void SetupGraph(DsDevice dev, int iFrameRate, int iWidth, int iHeight, string FileName)
{
int hr;
ISampleGrabber sampGrabber = null;
IBaseFilter baseGrabFlt = null;
IBaseFilter capFilter = null;
IBaseFilter muxFilter = null;
IFileSinkFilter fileWriterFilter = null;
ICaptureGraphBuilder2 capGraph = null;
// Get the graphbuilder object
m_FilterGraph = new FilterGraph() as IFilterGraph2;
m_mediaCtrl = m_FilterGraph as IMediaControl;
#if DEBUG
m_rot = new DsROTEntry(m_FilterGraph);
#endif
try
{
// Get the ICaptureGraphBuilder2
capGraph = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();
// Get the SampleGrabber interface
sampGrabber = (ISampleGrabber) new SampleGrabber();
// Start building the graph
hr = capGraph.SetFiltergraph( m_FilterGraph );
DsError.ThrowExceptionForHR( hr );
// Add the video device
hr = m_FilterGraph.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out capFilter);
DsError.ThrowExceptionForHR( hr );
baseGrabFlt = (IBaseFilter) sampGrabber;
ConfigureSampleGrabber(sampGrabber);
// Add the frame grabber to the graph
hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber");
DsError.ThrowExceptionForHR(hr);
// If any of the default config items are set
//if (iFrameRate + iHeight + iWidth > 0)
if (iFrameRate> 0)
{
SetConfigParms(capGraph, capFilter, iFrameRate, iWidth, iHeight);
}
// Create a filter for the output avi file
hr = capGraph.SetOutputFileName(MediaSubType.Avi, FileName, out muxFilter, out fileWriterFilter);
DsError.ThrowExceptionForHR( hr );
// Connect everything together
hr = capGraph.RenderStream( PinCategory.Capture, MediaType.Video, capFilter, baseGrabFlt, muxFilter);
DsError.ThrowExceptionForHR( hr );
// Now that sizes are fixed, store the sizes
SaveSizeInfo(sampGrabber);
}
finally
{
if (fileWriterFilter != null)
{
Marshal.ReleaseComObject(fileWriterFilter);
fileWriterFilter = null;
}
if (muxFilter != null)
{
Marshal.ReleaseComObject(muxFilter);
muxFilter = null;
}
if (capFilter != null)
{
Marshal.ReleaseComObject(capFilter);
capFilter = null;
}
if (sampGrabber != null)
{
Marshal.ReleaseComObject(sampGrabber);
sampGrabber = null;
}
}
}
/// <summary> Read and store the properties </summary>
private void SaveSizeInfo(ISampleGrabber sampGrabber)
{
int hr;
// Get the media type from the SampleGrabber
AMMediaType media = new AMMediaType();
hr = sampGrabber.GetConnectedMediaType( media );
DsError.ThrowExceptionForHR( hr );
if( (media.formatType != FormatType.VideoInfo) || (media.formatPtr == IntPtr.Zero) )
{
throw new NotSupportedException( "Unknown Grabber Media Format" );
}
// Grab the size info
VideoInfoHeader videoInfoHeader = (VideoInfoHeader) Marshal.PtrToStructure( media.formatPtr, typeof(VideoInfoHeader) );
m_videoWidth = videoInfoHeader.BmiHeader.Width;
m_videoHeight = videoInfoHeader.BmiHeader.Height;
m_stride = (m_videoWidth * (videoInfoHeader.BmiHeader.BitCount / 8));
DsUtils.FreeAMMediaType(media);
media = null;
}
/// <summary> Set the options on the sample grabber </summary>
private void ConfigureSampleGrabber(ISampleGrabber sampGrabber)
{
int hr;
AMMediaType media = new AMMediaType();
// Set the media type to Video/RBG24
media.majorType = MediaType.Video;
media.subType = MediaSubType.RGB24;
media.formatType = FormatType.VideoInfo;
hr = sampGrabber.SetMediaType( media );
DsError.ThrowExceptionForHR( hr );
DsUtils.FreeAMMediaType(media);
media = null;
// Configure the samplegrabber callback
hr = sampGrabber.SetCallback( this, 1 );
DsError.ThrowExceptionForHR( hr );
}
// Set the Framerate, and video size
private void SetConfigParms(ICaptureGraphBuilder2 capGraph, IBaseFilter capFilter, int iFrameRate, int iWidth, int iHeight)
{
int hr;
object o;
AMMediaType media;
IAMStreamConfig videoStreamConfig;
IAMVideoControl videoControl = capFilter as IAMVideoControl;
// Find the stream config interface
hr = capGraph.FindInterface(
PinCategory.Capture, MediaType.Video, capFilter, typeof(IAMStreamConfig).GUID, out o );
videoStreamConfig = o as IAMStreamConfig;
try
{
if (videoStreamConfig == null)
{
throw new Exception("Failed to get IAMStreamConfig");
}
hr = videoStreamConfig.GetFormat(out media);
DsError.ThrowExceptionForHR( hr );
// copy out the videoinfoheader
VideoInfoHeader v = new VideoInfoHeader();
Marshal.PtrToStructure( media.formatPtr, v );
// if overriding the framerate, set the frame rate
if (iFrameRate > 0)
{
v.AvgTimePerFrame = 10000000 / iFrameRate;
// v.AvgTimePerFrame = iFrameRate;
}
// if overriding the width, set the width
if (iWidth > 0)
{
v.BmiHeader.Width = iWidth;
}
// if overriding the Height, set the Height
if (iHeight > 0)
{
v.BmiHeader.Height = iHeight;
}
// Copy the media structure back
Marshal.StructureToPtr( v, media.formatPtr,false);
// Set the new format
hr = videoStreamConfig.SetFormat( media );
DsError.ThrowExceptionForHR( hr );
DsUtils.FreeAMMediaType(media);
media = null;
// Fix upsidedown video
if (videoControl != null)
{
VideoControlFlags pCapsFlags;
IPin pPin = DsFindPin.ByCategory(capFilter, PinCategory.Capture,0);
hr = videoControl.GetCaps(pPin, out pCapsFlags);
// DsError.ThrowExceptionForHR( hr );
if ((pCapsFlags & VideoControlFlags.FlipVertical) > 0)
{
hr = videoControl.GetMode(pPin, out pCapsFlags);
DsError.ThrowExceptionForHR(hr);
hr = videoControl.SetMode(pPin, 0);
}
}
}
finally
{
Marshal.ReleaseComObject(videoStreamConfig);
}
}
/// <summary> Shut down capture </summary>
private void CloseInterfaces()
{
int hr;
try
{
if( m_mediaCtrl != null )
{
// Stop the graph
hr = m_mediaCtrl.Stop();
m_mediaCtrl = null;
m_bRunning = false;
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
#if DEBUG
if (m_rot != null)
{
m_rot.Dispose();
}
#endif
if (m_FilterGraph != null)
{
Marshal.ReleaseComObject(m_FilterGraph);
m_FilterGraph = null;
}
GC.Collect();
}
/// <summary> sample callback, NOT USED. </summary>
int ISampleGrabberCB.SampleCB(double SampleTime, IMediaSample pSample)
{
Marshal.ReleaseComObject(pSample);
return 0;
}
/// <summary> buffer callback, COULD BE FROM FOREIGN THREAD. </summary>
int ISampleGrabberCB.BufferCB( double SampleTime, IntPtr pBuffer, int BufferLen )
{
// Avoid the possibility that someone is calling SetLogo() at this instant
lock (this)
{
if (m_bmdLogo != null)
{
IntPtr ipSource = m_bmdLogo.Scan0;
IntPtr ipDest = pBuffer;
for (int x=0; x <m_bmdLogo.Height; x++)
{
CopyMemory(ipDest, ipSource, (uint)m_bmdLogo.Stride);
//.RotateFlip(RotateFlipType.RotateNoneFlipY);
ipDest = ((IntPtr)(ipDest.ToInt32() + m_stride));
ipSource = (IntPtr)(ipSource.ToInt32() + m_bmdLogo.Stride);
}
}
}
return 0;
}
}
}
|
|
|
|
 |
|
|
 |
|
 |
how can modify this code to video watermarking?
|
|
|
|
 |
|
|
 |
|
 |
Many thanks, given you my 5.
Regards,
Ben
|
|
|
|
 |
|
|
 |
|
 |
I think you'll find it's a one way operation
|
|
|
|
 |
|
 |
Anyone know how to watermark each frame of an animated gif?
-Josh K.
Co-Founder/VP
www.GamerWealth.com
|
|
|
|
 |
|
 |
This is a great article, just one question though, is there an easy way to resize the image before saving it?
Thanks
|
|
|
|
 |
|
 |
cool, it's really understandable
and bug-free
with Best Regards
M.S. Babaei
|
|
|
|
 |
|
 |
This is it! This page saved me long hours of trying and searching on net.
Perfect, works great!
I just used semitransparent PNG file for the logo to save myself a few lines of code (I know, I can not change the transparency any more in the app, but I can update my watermark image easily)
Thanks, thanks, thanks!
|
|
|
|
 |
|
 |
Hello man it was a great code!
Can you tell me how can I do the subject question?
Thanks In Advance
KMILO
|
|
|
|
 |
|
 |
public static Bitmap ChangeAlpha(Bitmap bmp, int alphaPercent)
{
float newAlpha = (float)alphaPercent/100;
Bitmap newBmp = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format32bppArgb);
ImageAttributes imageAttri = new ImageAttributes();
float[][] colorMatrixElements = {
new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, newAlpha, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};
ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
imageAttri.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
using (Graphics g = Graphics.FromImage((Image)newBmp))
{
g.DrawImage((Image)bmp, new Rectangle(0,0, bmp.Width, bmp.Height), 0,0, bmp.Width, bmp.Height,
GraphicsUnit.Pixel, imageAttri);
}
imageAttri.Dispose();
return newBmp;
}
The code above is actually part of the overlay function. I just pulled it out and packaged it into an opacity function.
|
|
|
|
 |
|