Well, I try to write on an image in C#, my code is:
public string WriteOnImage(Bitmap Image, string NameImage, string TextFileName)
{
string Message = "OK";
try
{
Bitmap bitMapImage = new Bitmap(Image);
using (Graphics graphImage = Graphics.FromImage(Image))
{
graphImage.SmoothingMode = SmoothingMode.AntiAlias;
string line;
// Read the file and display it line by line.
StreamReader file = new StreamReader(Resources.C_PATH_DESTINO_IMG + TextFileName);
while ((line = file.ReadLine()) != null)
{
graphImage.DrawString(line, new Font("Courier New", 15, FontStyle.Bold), SystemBrushes.WindowText, new Point(0, 0));
HttpContext.Current.Response.ContentType = "image/jpeg";
bitMapImage.Save(Resources.C_PATH_DESTINO_IMG + NameImage, ImageFormat.Jpeg);
graphImage.Dispose();
bitMapImage.Dispose();
}
file.Close();
}
return Message;
}
catch (Exception ex)
{
EventLogWrite("Error: " + ex.Message);
return Message = ex.Message;
}
}
this method doesn't work because doesn't write on the image, please help me.
PD: I'm sorry for my english but I'm Latino jeje, thanks.
It looks like you are drawing on the wrong bitmap
Bitmap bitMapImage = new Bitmap(Image);
using (Graphics graphImage = Graphics.FromImage(Image))
should be
Bitmap bitMapImage = new Bitmap(Image);
using (Graphics graphImage = Graphics.FromImage(bitMapImage))
Related
I have an application that showing the pictures uploading from the system. For that I have to add watermark string into the images while uploading. When adding watermark to large size images, I receive “Parameter is not valid” when create image object from a memory stream.
What is the reason for this? Is there any other way to add watermark for large images.
using (FileStream fs = new FileStream(sourceFile.FullName, FileMode.Open, FileAccess.Read))
{
using (Stream output = new MemoryStream())
{
using (Image myImage = Image.FromStream(fs, false, false))
{
Font font = new Font(fontType, fontSize.Equals(0) ? 10 : fontSize, (FontStyle)fontStyle, GraphicsUnit.Pixel);
Color color = ColorTranslator.FromHtml(string.IsNullOrEmpty(fontColor) ? "#000000" : fontColor);
Point pt = new Point(10, 5);
SolidBrush brush = new SolidBrush(color);
using (Bitmap img = new Bitmap(new Bitmap(myImage)))
{
using (Graphics graphics = Graphics.FromImage(img))
{
graphics.DrawString(waterMarkText, font, brush, pt);
img.Save(output, GetImageFormatToDownload(sourceFile.Extension.ToUpper().Substring(1, sourceFile.Extension.Length - 1)));
using (Image imgFinal = Image.FromStream(output))
{
using (var bmp = new Bitmap(img.Width, img.Height, img.PixelFormat))
{
using (var finalGraphics = Graphics.FromImage(bmp))
{
finalGraphics.DrawImage(imgFinal, 0, 0, bmp.Width, bmp.Height);
bmp.Save(Path.Combine(storePath, storeName), GetImageFormatToDownload(sourceFile.Extension.ToUpper().Substring(1, sourceFile.Extension.Length - 1)));
sourceFile = new FileInfo(Path.Combine(storePath, storeName));
}
}
}
}
}
}
}
}
Thanks.
This is my code to resize an image. It works fine but when I try to delete the previously created, I have an error "file is used by another process". This is the code:
try
{
int newHeight = width * fromStream.Height / fromStream.Width;
Image newImage = new Bitmap(width, newHeight);
using (Graphics graphicsHandle = Graphics.FromImage(newImage))
{
graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphicsHandle.DrawImage(fromStream, 0, 0, width, newHeight);
}
string processedFileName = String.Concat(Configuration.CoverLocalPath, #"\Processed\res_", Path.GetFileName(imageFile));
newImage.Save(processedFileName, ImageFormat.Jpeg);
newImage.Dispose();
return processedFileName;
}
catch (Exception ex)
{
Configuration.Log.Debug("Utility.cs", "ResizeMainCover", ex.Message);
return string.Empty;
}
I tried to dispose the Image object but without success. Any hints?
Without more code, its hard to tell, but more than likely the culprit is your fromStream not being closed and disposed properly. I'm assuming "previously created" means your source stream. Try wrapping it in a using statement, note I also wrapped the newImage so it would be disposed properly in case of an Exception.
using(var fromStream = GetSourceImageStream())
{
try
{
int newHeight = width * fromStream.Height / fromStream.Width;
using(Image newImage = new Bitmap(width, newHeight))
{
using (Graphics graphicsHandle = Graphics.FromImage(newImage))
{
graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphicsHandle.DrawImage(fromStream, 0, 0, width, newHeight);
}
string processedFileName = String.Concat(Configuration.CoverLocalPath, #"\Processed\res_", Path.GetFileName(imageFile));
newImage.Save(processedFileName, ImageFormat.Jpeg);
}
return processedFileName;
}
catch (Exception ex)
{
Configuration.Log.Debug("Utility.cs", "ResizeMainCover", ex.Message);
return string.Empty;
}
finally
{
fromStream.Close();
}
}
I'm creating a web app that generates barcodes that uses information that's databound. That information is a name from a database.
The barcode generates correctly, but I want to add text to it.
Here is my code for the barcode generator.
protected void btnGenerate_Click(object sender, EventArgs e)
{
foreach (ListItem item in BarCode.Items)
{
if (item.Selected)
{
string barCode = Barcode + txtCode.Text;
System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
using (Bitmap bitMap = new Bitmap(barCode.Length * 50, 90))
{
using (Graphics graphics = Graphics.FromImage(bitMap))
{
Font oFont = new Font("IDAutomationHC39M", 18);
PointF point = new PointF(3f, 3f);
SolidBrush blackBrush = new SolidBrush(Color.Black);
SolidBrush whiteBrush = new SolidBrush(Color.White);
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
graphics.DrawString(barCode, oFont, blackBrush, point);
}
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
Convert.ToBase64String(byteImage);
imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
}
plBarCode.Controls.Add(imgBarCode);
}
}
}
}
I want to add
"QTY:_____________"
underneath the barcode when it's generated. Although I believe the formatting of the barcode is limited to the code and I don't believe I can create a string literal
string lit = "QTY:_______________";
and add it to the barcode string:
string barCode = Barcode + txtCode.Text + Environement.NewLine + lit;
Is it possible to add that underneath programmatically?
If I understand you right You don't need to add text into code, you need to print some text under bar code. Just add into:
using (Graphics graphics
somthig like this:
// put coordinates here:
RectangleF rectf = new RectangleF(70, 90, 90, 50);
graphics.DrawString(lit , new Font("Tahoma",8), Brushes.Black, rectf);
I am trying for create a barcode in my web form.for that i download font IDAutomationHC39M and install in my system,then i run my wesite in localhost but barcode cannot be generated.This is my code
protected void Button1_Click1(object sender, EventArgs e)
{
string barCode = TextBox1.Text;
System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
using (Bitmap bitMap = new Bitmap(barCode.Length * 40, 80))
{
using (Graphics graphics = Graphics.FromImage(bitMap))
{
Font oFont = new Font("IDAutomationHC39M", 16);
PointF point = new PointF(2f, 2f);
SolidBrush blackBrush = new SolidBrush(Color.Black);
SolidBrush whiteBrush = new SolidBrush(Color.White);
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
graphics.DrawString("*" + barCode + "*", oFont, blackBrush, point);
}
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
Convert.ToBase64String(byteImage);
imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
}
plBarCode.Controls.Add(imgBarCode);
}
And the result is appear like that code
Does any body know how to get a thumbnail/snapshot of a specific frame of a smooth streaming file using C#.net and WPF.
Regards,
Allan
Here MyPanel is the container where your video is streaming.
var panelPoint = this.MyPanel.PointToScreen(new Point(this.MyPanel.ClientRectangle.X, this.MyPanel.ClientRectangle.Y));
using (var bitmap = new Bitmap(320, 240))
{
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(320, Point.Empty, new Size(320, 240));
}
if (SimpleIoc.Default.ContainsCreated<ICommonApplicationData>())
{
var imageGuidName = Guid.NewGuid();
fileName = Path.Combine("C:\", "TestFolder", imageGuidName + ".jpg");
bitmap.Save(fileName, ImageFormat.Jpeg);
var tempBitmapImage = new BitmapImage();
tempBitmapImage.BeginInit();
tempBitmapImage.UriSource = new Uri(fileName);
tempBitmapImage.EndInit();
image.Source = tempBitmapImage;
}
}