0
I am new in the world . NET and I have a problem sending images to the site. I am using a function to add Watermark to the photo as soon as it is sent but apparently Azure no longer accepts the use of System.Drawing.Bitmap. I tried to find some alternative but could not, any suggestions? Below the code:
private void AdicionarMarcaDagua(HttpPostedFileBase arquivo, string caminhoTemp, string caminhoFotos, string caminhoThumbs, int id)
{
string Copyright = "www.vidaideal.com";
Image imgPhotoTemp = Image.FromFile(caminhoTemp + arquivo.FileName);
int novaAltura = 600;
int novaLargura = 800;
Bitmap novaImagem = new Bitmap(novaLargura, novaAltura);
Graphics g = Graphics.FromImage((Image)novaImagem);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(imgPhotoTemp, 0, 0, novaLargura, novaAltura);
g.Dispose();
novaImagem.Save(caminhoTemp + id +"_" + arquivo.FileName);
imgPhotoTemp.Dispose();
Image imgPhoto = Image.FromFile(caminhoTemp + id + "_" + arquivo.FileName);
int phWidth = imgPhoto.Width;
int phHeight = imgPhoto.Height;
Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
Image imgWatermark = new Bitmap(Server.MapPath("~/Images/Layout/") + "\\watermark-color.png");
int wmWidth = imgWatermark.Width;
int wmHeight = imgWatermark.Height;
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, phWidth, phHeight), 0, 0, phWidth, phHeight, GraphicsUnit.Pixel);
int[] sizes = new int[] { 16, 14, 12, 10, 8, 6, 4 };
Font crFont = null;
SizeF crSize = new SizeF();
for (int i = 0; i < 7; i++)
{
crFont = new Font("Verdana", sizes[i], FontStyle.Bold);
crSize = grPhoto.MeasureString(Copyright, crFont);
if ((ushort)crSize.Width < (ushort)phWidth)
break;
}
int yPixlesFromBottom = (int)(phHeight * .05);
float yPosFromBottom = ((phHeight - yPixlesFromBottom) - (crSize.Height / 2));
float xCenterOfImg = (phWidth / 2);
StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Center;
SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));
grPhoto.DrawString(Copyright, crFont, semiTransBrush2, new PointF(xCenterOfImg + 1, yPosFromBottom + 1), StrFormat);
SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
grPhoto.DrawString(Copyright, crFont, semiTransBrush, new PointF(xCenterOfImg, yPosFromBottom), StrFormat);
Bitmap bmWatermark = new Bitmap(bmPhoto);
bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
Graphics grWatermark = Graphics.FromImage(bmWatermark);
ImageAttributes imageAttributes = new ImageAttributes();
ColorMap colorMap = new ColorMap();
colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
ColorMap[] remapTable = { colorMap };
imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
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);
int xPosOfWm = ((phWidth - wmWidth) - 10);
int yPosOfWm = 10;
grWatermark.DrawImage(imgWatermark, new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight), 0, 0, wmWidth, wmHeight, GraphicsUnit.Pixel, imageAttributes);
imgPhoto = bmWatermark;
grPhoto.Dispose();
grWatermark.Dispose();
imgPhoto.Save(caminhoFotos + id + "_" + arquivo.FileName, ImageFormat.Jpeg);
imgPhoto.Dispose();
imgWatermark.Dispose();
GerarThumb(caminhoFotos, arquivo, caminhoThumbs, id);
}
I tried to find some documentation in Azure itself but could not find, I saw something about using Blob but at first I want to avoid the use of this resource because it increases the monthly cost of operation without need.
The . NET being used is the 4.7.1 Full, C# language with MVC 5.
Apart from the part that is not the correct way to make this code the Azure not determined anything, the . NET you are using can be. O . NET Core before version 3.0 (coming out next year) does not have the
System.Drawing
so you need to find other libraries for use now. There are people who use Skiasharp, but I don’t know if it’s good or meets you, or even if.– Maniero
Hello Maniero, all right? Dude, how come it’s not the right way? I upload and call the method to add the Watermark and resize the image, is this form not correct? I am using the . NET 4.7.1 and running locally works perfectly, it is when I publish in Azure that gives parameter error, looking in the image folders it even sends, resize but stops in Watermark.
– Cristiano
It has a huge amount of errors, you made it work, but it’s not right. It gives a search on the site that has information on. You call the
Dispose()
manually and "can’t", there are things that should have the same treatment and not have, there are things that work if everything is right, but it breaks the application if it is not. Also look here for the difference between . NET Framework and . NET Core.– Maniero
Maniero, I added Dispose to clear the memory and I can delete the temporary files but in theory it is not the problem because the method stops running in Watermark’s Bitmap. What mistakes do you refer to? If you can point out what is wrong I thank you because as I said I am new in the . NET and could not identify errors in the code. Now what has the difference between . NET and Core? I’m not using Core, I didn’t get your point regarding this.
– Cristiano
I just told you what mistakes they are. And there are many. As I’m starting out I think you’re doing things that are too advanced, it would be better to understand the basics before moving on to the advanced. So you’re using the . NET Framework full in Azure? Are you sure about this? Your question doesn’t make it clear, which indicates that it should be closed.
– Maniero
I’m sorry Maniero but I didn’t see any errors pointed out, just the unnecessary use of Dispose and gave a reason for such use. I know I need to understand the basics, but right now what I need is this information. As I reported in my first interaction, I’m using . NET 4.7.1 - I said, I didn’t say "I think". Really not to inform on the question was a forgetfulness of mine and I will edit now. Starting from the premise that the Runtime indicates errors and the code compiles, I do not know identify what the problem of this code so if it can be more specific about the various errors I appreciate immensely.
– Cristiano
Only a small correction: it lacked the no in the last sentence, the Runtime does not accuse errors and compiles normally. Locally runs without any problem the entire application.
– Cristiano