2016-11-29 4 views
0

J'essaie d'ajouter deux textes de filigrane sur les images, l'un en bas à gauche et l'autre en bas à droite de l'image quelles que soient les dimensions de l'image. À la suite de ma méthode:Taille du texte du filigrane sur l'image avec les mêmes dimensions mais différentes ppp

public void AddWaterMark(string leftSideText, string rightSideText, string imagePath) 
{ 
    string firstText = leftSideText; 
    string secondText = rightSideText; 

    Bitmap bitmap = (Bitmap)Image.FromFile(imagePath);//load the image file 

    PointF firstLocation = new PointF((float)(bitmap.Width * 0.035), bitmap.Height - (float)(bitmap.Height * 0.06)); 
    PointF secondLocation = new PointF(((float)((bitmap.Width/2) + ((bitmap.Width/2) * 0.6))), bitmap.Height - (float)(bitmap.Height * 0.055)); 

    int opacity = 155, baseFontSize = 50; 
    int leftTextSize = 0, rightTextSize = 0; 
    leftTextSize = (bitmap.Width * baseFontSize)/1920; 
    rightTextSize = leftTextSize - 5; 
    using (Graphics graphics = Graphics.FromImage(bitmap)) 
    { 
     Font arialFontLeft = new Font(FontFamily.GenericSerif, leftTextSize); 
     Font arialFontRight = new Font(FontFamily.GenericSerif, rightTextSize); 
     graphics.DrawString(firstText, arialFontLeft, new SolidBrush(Color.FromArgb(opacity, Color.White)), firstLocation); 
     graphics.DrawString(secondText, arialFontRight, new SolidBrush(Color.FromArgb(opacity, Color.White)), secondLocation); 
    } 
    string fileLocation = HttpContext.Current.Server.MapPath("~/Images/Albums/") + Path.GetFileNameWithoutExtension(imagePath) + "_watermarked" + Path.GetExtension(imagePath); 
    bitmap.Save(fileLocation);//save the image file 
    bitmap.Dispose(); 
    if (File.Exists(imagePath)) 
    { 
     File.Delete(imagePath); 
     File.Move(fileLocation, fileLocation.Replace("_watermarked", string.Empty)); 
    } 
} 

Le problème que je suis confronté est à la mise en font size du texte de marque de l'eau correctement. Disons qu'il y a deux images avec des dimensions de 1600 x 900 pixels et la première image a un dpi de 72 et la deuxième image a dpi de 240. La méthode ci-dessus fonctionne correctement pour l'image avec 72 ppp mais pour l'image avec 240dpi, le font size du texte du filigrane devient trop grand et déborde sur l'image. Comment calculer font size correctement avec des images de différents dpi mais a les mêmes dimensions?

+0

Vous souhaitez donc que la police soit plus petite pour les valeurs PPP plus grandes? (Taille de la police en pixels plutôt qu'en fonction de DPI) – grek40

Répondre

1

Cette astuce simple devrait fonctionner:

Avant appliquer le texte mis l'dpi de l'image. Après application du texte réinitialiser aux valeurs précédentes.

float dpiXNew = 123f; 
float dpiYNew = 123f; 

float dpiXOld = bmp.HorizontalResolution; 
float dpiYOld = bmp.VerticalResolution; 

bmp.SetResolution(dpiXNew, dpiYNew); 

using (Graphics g = Graphics.FromImage(bmp)) 
{ 
    TextRenderer.DrawText(g, "yourText", ....) 
    ... 
} 

bmp.SetResolution(dpiXOld, dpiYOld);