How to get the imagestream from graphics class - c#

I want to save the image created from graphics class how to do it?
(if i can get the imagestream then i can do it using below code)
var bmp = Bitmap.FromStream(originalStream);
// var newImage = new Bitmap(bmp.Width, bmp.Height);
var img = new Bitmap(bmp,
(int)(bmp.Size.Width / zoomLevel),
(int)(bmp.Size.Height / zoomLevel));
var g = Graphics.FromImage(img);
g.DrawImageUnscaled(bmp, (int)CurrentX, (int)CurrentY);
g.DrawString(Text, SystemFonts.DefaultFont, Brushes.Black, (float)CurrentTextX, (float)CurrentTextY);
string originalImagePath = string.Concat(#"Photos/", string.Format("{0:yyyy-MM-dd}", ApplicationDateTime.Today) + "/" + Session[Keys.UserId], "/", url.Split('/').Last());
var originalImageamazonResponse = _s3AmazonServices.StoreImage(originalStream, originalImagePath, Configuration.ConfigurationNew.Current.S3Bucket, Configuration.ConfigurationNew.Current.S3AmazonAccessKey, Configuration.ConfigurationNew.Current.S3AmazonSecretKey);

I believe that img gets modified with everything you did to the g that got created from it. So you should be able to call img.Save().

Related

Why getting “Parameter is not valid” exception when creating an Image object from Memory stream of an image which size is greater that 100MB in C#?

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.

Adding String Literal to Generated Barcode image

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);

How to display the column1 text on column2 image in Gridview using c#

I have two columns in my database(Column 1: text ,column 2: image). Now I need to Display image, and on that image I need to display column1's text, Finally the Result should be like this image
enter image description here
Here Healthy foods is my column1's text and background is column2's image.
I am using C# and Winforms.
After bringing your data from database to datatable after
byte[] fileContents = (byte[])dts.Rows[0]["image"];
var memoryStream = new MemoryStream(fileContents);
var biId = new Bitmap(memoryStream);
Bitmap bitMapImage = new System.Drawing.Bitmap(biId);
Graphics graphicImage = Graphics.FromImage(bitMapImage);
graphicImage.SmoothingMode = SmoothingMode.HighQuality;
SolidBrush blueBrush = new SolidBrush(Color.Blue);
SolidBrush redBrush = new SolidBrush(Color.Red);
SolidBrush greenBrush = new SolidBrush(Color.Green);
RectangleF yourtextmessage= new RectangleF(285, 20, 250, 250);
graphicImage.DrawString(dts.Rows[0]["text1"].ToString() + " ,", new Font("Verdana", 12, FontStyle.Italic), Brushes.White, yourtextmessage);
context.Response.ContentType = "image/jpeg";
bitMapImage.Save(context.Response.OutputStream, ImageFormat.Jpeg);
graphicImage.Dispose();
bitMapImage.Dispose();

Generated Barcode Image not getting scanned through scanner in asp.net web application

I have generated dynamic barcode image in asp.net web application but its not getting scanned/reading through barcode scanner.
Below is the code where i have generated barcode image.kindly find it out.
Need Your Suggestion and help,awaiting your response.
string barCode = LblSerialNo.Text;
System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
using (Bitmap bitMap = new Bitmap(barCode.Length * 11, 45))
{
using (Graphics graphics = Graphics.FromImage(bitMap))
{
Font oFont = new Font("IDAutomationHC39M", 8);
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);
}

Get Thumbnail from a smooth streaming file (.ism)

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;
}
}

Categories

Resources