本文整理汇总了C#中System.Drawing.Bitmap.SetResolution方法的典型用法代码示例。如果您正苦于以下问题:C# System.Drawing.Bitmap.SetResolution方法的具体用法?C# System.Drawing.Bitmap.SetResolution怎么用?C# System.Drawing.Bitmap.SetResolution使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Drawing.Bitmap
的用法示例。
在下文中一共展示了System.Drawing.Bitmap.SetResolution方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ToBitmap
public System.Drawing.Bitmap ToBitmap()
{
#if WindowsCE
var bmp = new System.Drawing.Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
var bmpData = bmp.LockBits(
new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
System.Drawing.Imaging.ImageLockMode.WriteOnly,
System.Drawing.Imaging.PixelFormat.Format32bppRgb);
#else
var bmp = new System.Drawing.Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
bmp.SetResolution(96, 96);
var bmpData = bmp.LockBits(
new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
System.Drawing.Imaging.ImageLockMode.WriteOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
#endif
try
{
//Copy the data from the byte array into BitmapData.Scan0
System.Runtime.InteropServices.Marshal.Copy(Pixels, 0, bmpData.Scan0, Pixels.Length);
}
finally
{
//Unlock the pixels
bmp.UnlockBits(bmpData);
}
return bmp;
}
示例2: CropImage
public string CropImage(int x, int y, int w, int h, string imagePath)
{
var pathImage = Server.MapPath(imagePath);
Image img = Image.FromFile(pathImage);
Bitmap newBitmap = null;
using (System.Drawing.Bitmap _bitmap = new System.Drawing.Bitmap(w, h))
{
_bitmap.SetResolution(img.HorizontalResolution, img.VerticalResolution);
using (Graphics _graphic = Graphics.FromImage(_bitmap))
{
_graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
_graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
_graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
_graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
_graphic.DrawImage(img, 0, 0, w, h);
_graphic.DrawImage(img, new Rectangle(0, 0, w, h), x, y, w, h, GraphicsUnit.Pixel);
}
var objImg = _bitmap.Clone();
newBitmap = (Bitmap)objImg;
}
var fileName = Path.GetFileNameWithoutExtension(imagePath) + "_Cropped" + Path.GetExtension(imagePath);
newBitmap.Save(Server.MapPath("/Uploads/Temp/") + fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
return fileName;
}
示例3: Image_MouseLeftButtonUp
private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
image.ReleaseMouseCapture();
// Get actual rectangle
if( selectBox.Visibility != Visibility.Visible)
return;
Vector imageOffset = VisualTreeHelper.GetOffset(image);
double visualLeft = Canvas.GetLeft( selectBox );
double visualTop = Canvas.GetTop(selectBox);
double width = selectBox.Width;
double height = selectBox.Height;
BitmapSource bmpSource = image.Source as BitmapSource;
double scaleX = bmpSource.PixelWidth / image.ActualWidth;
double scaleY = bmpSource.PixelHeight / image.ActualHeight;
double newWidth = width * scaleX;
double newHeight = height * scaleY;
double newX = (visualLeft - imageOffset.X) * scaleX;
double newY = (visualTop - imageOffset.Y) * scaleY;
System.Drawing.Bitmap src = System.Drawing.Image.FromFile(_sourceFile) as System.Drawing.Bitmap;
var target = new System.Drawing.Bitmap((int)newWidth, (int)newHeight);
target.SetResolution(src.HorizontalResolution, src.VerticalResolution);
using (var g = System.Drawing.Graphics.FromImage(target))
{
var rect = new System.Drawing.Rectangle((int)newX, (int)newY, (int)newWidth, (int)newHeight);
g.DrawImage(src, new System.Drawing.Rectangle(0, 0, target.Width, target.Height),
rect,
System.Drawing.GraphicsUnit.Pixel);
}
RegionSelectedEventArgs ev = new RegionSelectedEventArgs(new Rect(newX, newY, newWidth, newHeight), target);
OnRegionSelected(ev);
selectBox.Visibility = Visibility.Hidden;
}
示例4: Dothumb
private System.Drawing.Bitmap Dothumb(System.Drawing.Image imgPhoto, int Width, int Height)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)Width / (float)sourceWidth);
nPercentH = ((float)Height / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = System.Convert.ToInt16((Width - (sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((Height - (sourceHeight * nPercent)) / 2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
System.Drawing.Bitmap bmPhoto = new System.Drawing.Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
System.Drawing.Graphics grPhoto = System.Drawing.Graphics.FromImage(bmPhoto);
grPhoto.Clear(System.Drawing.Color.Red);
grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto, new System.Drawing.Rectangle(destX, destY, destWidth, destHeight),
new System.Drawing.Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
System.Drawing.GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
示例5: SaveCardsAsJpegsForPrinting
/// <summary>
/// Saves cards with QSO info as JPEGs for photo printing
/// </summary>
/// <param name="startNumber">largest number (nnn) in file names QSLnnn.jpeg in print directory before printing starts</param>
/// <param name="directoryName">path to directory that JPEG files are to be written to</param>
/// <exception>DirectoryNotFoundException when directory has been deleted between time directory was selected and attempt
/// to create or write file.</exception>
/// <exception>SecurityException when user does not permission to access the directory or file (e.g. write permission on directory.</exception>
/// <exception>IOException for an IO error</exception>
/// <exception>PathTooLongException when the directory is longer than 248 characters, or filename is longer than 260 characters.</exception>
/// <exception>Several others that definitely should never occur.</exception>
private void SaveCardsAsJpegsForPrinting(int startNumber, string directoryName)
{
CardTabItem cti = mainTabControl.SelectedItem as CardTabItem;
if(cti != null)
{
//create a list of QSOs for each card
List<List<DispQso>> qsos = qsosView.DisplayQsos.GetDispQsosList(cti.cardPanel.QslCard);
// now create file for each card
for(int cardNum = 0; cardNum < qsos.Count; cardNum++)
{
CardWF card = cti.cardPanel.QslCard.Clone();
card.IsInDesignMode = false;
FormsCardView cView = new FormsCardView(card);
// inform user of progress
StatusText.Text = "Creating card " + (cardNum+1) + " of " + qsos.Count;
StatusText.InvalidateVisual();
ForceUIUpdate();
// create the card and render it
float scale = (float)300F / 100.0F;
int bitmapWidth = (int)((float)card.Width * scale);
int bitmapHeight = (int)((float)card.Height * scale);
System.Drawing.Bitmap bitmap =
new System.Drawing.Bitmap(bitmapWidth, bitmapHeight);
bitmap.SetResolution(300F, 300F);
System.Drawing.Graphics graphics =
System.Drawing.Graphics.FromImage(bitmap);
graphics.ScaleTransform(scale, scale);
cView.PaintCard(graphics, qsos[cardNum], 96F / 300F);
graphics.Dispose();
string fileName = directoryName + "\\Qsl" +
(startNumber + cardNum + 1) + ".jpg";
bitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();
// force garbage collection to prevent Out of Memory exception
if(cardNum % 50 == 0)
{
System.GC.WaitForPendingFinalizers();
System.GC.Collect();
}
}
// tell user that all cards are created
StatusText.Text = qsos.Count + " of " + qsos.Count + " cards created in directory: "
+ directoryName;
}
}
示例6: SaveCardAsJpeg
/// <summary>
/// Save a card as JPEG file
/// </summary>
/// <param name="fileName">path to the file to save</param>
/// <param name="resolution">image resolution</param>
/// <param name="quality">image quality</param>
/// <param name="showQsos">Boolean to indicate whether the card should include QSO info</param>
private void SaveCardAsJpeg(string fileName, int resolution, int quality,
bool showQsos)
{
// create visual of the card
CardTabItem cti = mainTabControl.SelectedItem as CardTabItem;
if(cti != null)
{
CardWF card = cti.cardPanel.QslCard.Clone();
card.IsInDesignMode = false;
FormsCardView cView = new FormsCardView(card);
List<List<DispQso>> dispQsos = new List<List<DispQso>>();
if(card.QsosBox != null)
{
if(qsosView.DisplayQsos.Count > 0 && showQsos)
{
dispQsos = qsosView.DisplayQsos.GetDispQsosList(card);
}
}
float scale = (float)resolution / 100.0F;
int bitmapWidth = (int)((float)card.Width * scale);
int bitmapHeight = (int)((float)card.Height * scale);
System.Drawing.Bitmap bitmap =
new System.Drawing.Bitmap(bitmapWidth, bitmapHeight);
bitmap.SetResolution(resolution, resolution);
System.Drawing.Graphics graphics =
System.Drawing.Graphics.FromImage(bitmap);
graphics.ScaleTransform(scale, scale);
cView.PaintCard(graphics, dispQsos.Count > 0 ? dispQsos[0] : null,
96F / resolution);
graphics.Dispose();
System.Drawing.Imaging.ImageCodecInfo jpgEncoder =
GetEncoder(System.Drawing.Imaging.ImageFormat.Jpeg);
System.Drawing.Imaging.Encoder qualityEncoder = System.Drawing.Imaging.Encoder.Quality;
System.Drawing.Imaging.EncoderParameter encoderParameter =
new System.Drawing.Imaging.EncoderParameter(qualityEncoder, quality);
System.Drawing.Imaging.EncoderParameters encoderParams =
new System.Drawing.Imaging.EncoderParameters(1);
encoderParams.Param[0] = encoderParameter;
bitmap.Save(fileName, jpgEncoder, encoderParams);
bitmap.Dispose();
}
}
示例7: Save4UpAsJpeg
/// <summary>
/// Helper method that saves 4 Up Cards in a JPEG file
/// </summary>
/// <param name="fileName">name of file to store image in</param>
/// <param name="resolution">Resolution to store the image at</param>
/// <param name="incBorders">Boolean indicating whether card edges should be included</param>
private void Save4UpAsJpeg(string fileName, float resolution, bool incBorders)
{
// create visual of the card
// create the card and render it
CardTabItem cti = mainTabControl.SelectedItem as CardTabItem;
CardWF cardToSave = cti.cardPanel.QslCard;
float scale = resolution / 100.0F;
int bitmapWidth = (int)((float)cardToSave.Width * scale * 2);
int bitmapHeight = (int)((float)cardToSave.Height * scale * 2);
System.Drawing.Bitmap bitmap =
new System.Drawing.Bitmap(bitmapWidth, bitmapHeight);
bitmap.SetResolution(resolution, resolution);
System.Drawing.Graphics graphics =
System.Drawing.Graphics.FromImage(bitmap);
graphics.ScaleTransform(scale, scale);
for (int cardNo = 0; cardNo <= 3; cardNo++)
{
CardWF card = cardToSave.Clone();
card.IsInDesignMode = false;
card.CardPrintProperties.PrintCardOutlines = incBorders;
FormsCardView cView = new FormsCardView(card);
System.Drawing.Drawing2D.GraphicsState state = graphics.Save();
float offX = (cardNo % 2 == 0) ? 0 : card.Width;
float offY = (cardNo < 2) ? 0 : card.Height;
graphics.TranslateTransform(offX, offY);
cView.PaintCard(graphics, null, 96F / resolution);
graphics.Restore(state);
}
graphics.Dispose();
bitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();
}
示例8: ResizeImageFile
public byte[] ResizeImageFile(byte[] imageFile, int targetWidth, int targetHeight)
{
System.Drawing.Image original = System.Drawing.Image.FromStream(new System.IO.MemoryStream(imageFile));
int targetH, targetW;
// if it is too tall and tall and skinny
if (original.Height > targetHeight && ((float)original.Height / (float)original.Width) > ((float)targetHeight / (float)targetWidth))
{
targetH = targetHeight;
targetW = (int)(original.Width * ((float)targetHeight / (float)original.Height));
}
// if it is too wide
else if (original.Width > targetWidth)
{
targetW = targetWidth;
targetH = (int)(original.Height * ((float)targetWidth / (float)original.Width));
}
else // leave it as is
{
targetH = original.Height;
targetW = original.Width;
}
System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(new System.IO.MemoryStream(imageFile));
// Create a new blank canvas. The resized image will be drawn on this canvas.
System.Drawing.Bitmap bmPhoto = new System.Drawing.Bitmap(targetW, targetH, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(72, 72);
System.Drawing.Graphics grPhoto = System.Drawing.Graphics.FromImage(bmPhoto);
grPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
grPhoto.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
grPhoto.DrawImage(imgPhoto, new System.Drawing.Rectangle(0, 0, targetW, targetH), 0, 0, original.Width, original.Height, System.Drawing.GraphicsUnit.Pixel);
// Save out to memory and then to a file. We dispose of all objects to make sure the files don't stay locked.
System.IO.MemoryStream mm = new System.IO.MemoryStream();
bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
original.Dispose();
imgPhoto.Dispose();
bmPhoto.Dispose();
grPhoto.Dispose();
return mm.GetBuffer();
}
示例9: btnUpload_Click
protected void btnUpload_Click(object sender, EventArgs e)
{
if(flUploadReplace.HasFile) {
try {
Extension = flUploadReplace.FileName.Substring(flUpload.FileName.LastIndexOf(".") + 1);
Extension = "png";
var fileName = (Server.MapPath(Folder) + FileName + "." + Extension);
//flUpload.SaveAs(fileName);
lblUplderr.Text = "<br>File size: " +
flUploadReplace.PostedFile.ContentLength + " kb<br>" +
"Content type: " +
flUploadReplace.PostedFile.ContentType;
var upImage = System.Drawing.Image.FromStream(flUploadReplace.PostedFile.InputStream);
// Get height and width of current image
int currWidth = (int)upImage.Width;
int currHeight = (int)upImage.Height;
Int32 newWidth = ImgWidth;
float ratio = 0;
Int32 newHeight = 0;
ratio = (float)currWidth / (float)newWidth;
newHeight = (int)(currHeight / ratio);
var newBmp = new System.Drawing.Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
newBmp.SetResolution(72, 72);
newBmp.MakeTransparent();
var newGraphic = System.Drawing.Graphics.FromImage(newBmp);
//newGraphic.Clear(Color.DarkRed);\
newGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
newGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
newGraphic.DrawImage(upImage, 0, 0, newWidth, newHeight);
newBmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
newBmp.Dispose();
newGraphic.Dispose();
if(CreateSmallThumbnail) {
fileName = Server.MapPath(Folder) + "\\" + SmallThumbnailPrefix + FileName + "." + Extension;
newWidth = SmallThumbnailWidth;
ratio = (float)currWidth / (float)newWidth;
newHeight = (int)(currHeight / ratio);
newBmp = new System.Drawing.Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
newBmp.SetResolution(72, 72);
newBmp.MakeTransparent();
newGraphic = System.Drawing.Graphics.FromImage(newBmp);
//newGraphic.Clear(Color.DarkRed);
newGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
newGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
newGraphic.DrawImage(upImage, 0, 0, newWidth, newHeight);
newBmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
newBmp.Dispose();
newGraphic.Dispose();
}
if(CreateMediumThumbnail) {
fileName = Server.MapPath(Folder) + "\\" + MediumThumbnailPrefix + FileName + "." + Extension;
newWidth = MediumThumbnailWidth;
ratio = (float)currWidth / (float)newWidth;
newHeight = (int)(currHeight / ratio);
newBmp = new System.Drawing.Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
newBmp.SetResolution(72, 72);
newBmp.MakeTransparent();
newGraphic = System.Drawing.Graphics.FromImage(newBmp);
//newGraphic.Clear(Color.DarkRed);
newGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
newGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
newGraphic.DrawImage(upImage, 0, 0, newWidth, newHeight);
newBmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
newBmp.Dispose();
newGraphic.Dispose();
}
upImage.Dispose();
// resize
// thumb
// medium
ProcessRender();
} catch(Exception ex) {
lblUplderr1.Text = "<br><font color=red>ERROR: " + ex.Message.ToString() + "</font>";
}
} else {
lblUplderr1.Text = "<br><font color=red>ERROR: You have not specified a file.</font>";
}
}
示例10: Crop
private System.Drawing.Bitmap Crop(string img, int width, int height, int x, int y)
{
try
{
System.Drawing.Image image = System.Drawing.Image.FromFile(img);
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bmp.SetResolution(80, 60);
System.Drawing.Graphics gfx = System.Drawing.Graphics.FromImage(bmp);
gfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
gfx.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gfx.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
gfx.DrawImage(image, new System.Drawing.Rectangle(0, 0, width, height), x, y, width, height, System.Drawing.GraphicsUnit.Pixel);
gfx.Dispose();
image.Dispose();
return bmp;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}
示例11: toolStripMenuItem1_Click
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
System.Drawing.Image img = CaptureActiveWindow();
img.Save(@thefilename, System.Drawing.Imaging.ImageFormat.Jpeg);
//markus' tweet code
Random random = new Random();
int number = random.Next(0, 100);
//string base64String;
byte[] imageBytes;
/* using (System.Drawing.Bitmap bm = new System.Drawing.Bitmap(thefilename))
{
using (MemoryStream ms = new MemoryStream())
{
bm.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
base64String = Convert.ToBase64String(ms.ToArray());
}
}*/
MemoryStream ms = new MemoryStream();
using (System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)System.Drawing.Image.FromFile(thefilename))
{
using (System.Drawing.Bitmap newBitmap = new System.Drawing.Bitmap(bitmap))
{
newBitmap.SetResolution(300, 300); //que?
newBitmap.Save(@thefilename_res, System.Drawing.Imaging.ImageFormat.Jpeg);
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
imageBytes = ms.ToArray();
}
}
TwitPic tw = new TwitPic();
Debug.WriteLine("image/jpeg " + "image_ " + number + " " + thefilename_res + " " + "f1df26cd49afe58d92fff17cdd1c94bf " + "494611009-ybbVnZ9ThmVUhN65QvH0x2l48BcXUtF0pNG8AUq4 " + "YZ2eGUbGnYz1ratWweQD1fpK1JuxAUtJ4nIZBA1Y " + "cflG9inzlNltp2Znw5zEWA " + "CDS4bQi9NRdRRBGR4Am1skJNRonHbGrsBFwUmpk ");
string upload_script = tw.UploadPhoto(imageBytes, "image/jpeg", "image_" + number, thefilename_res, "f1df26cd49afe58d92fff17cdd1c94bf", "494611009-ybbVnZ9ThmVUhN65QvH0x2l48BcXUtF0pNG8AUq4", "YZ2eGUbGnYz1ratWweQD1fpK1JuxAUtJ4nIZBA1Y", "cflG9inzlNltp2Znw5zEWA", "CDS4bQi9NRdRRBGR4Am1skJNRonHbGrsBFwUmpk").ToString();
Debug.WriteLine("Result of upload_script =" + upload_script);
}
示例12: resizeImage
public static System.Drawing.Image resizeImage(System.Drawing.Image imgPhoto, int Percent)
{
float nPercent = ((float)Percent / 100);
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
System.Drawing.Bitmap bmPhoto = new System.Drawing.Bitmap(destWidth, destHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
System.Drawing.Graphics grPhoto = System.Drawing.Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new System.Drawing.Rectangle(destX, destY, destWidth, destHeight),
new System.Drawing.Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
System.Drawing.GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
示例13: cbUpload_MouseDown
private void cbUpload_MouseDown(object sender, MouseButtonEventArgs e)
{
if (!isBusy)
{
isBusy = true;
try
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = ".jpg";
dlg.Filter = "Image Files (*.jpeg,*.jpg,*.png)|*.jpeg;*.png;*.jpg";
cam.VideoSourcePlayer.Stop();
imgSnap.Source = null;
cropArea.Visibility = Visibility.Visible;
cam.Visibility = Visibility.Hidden;
Nullable<bool> result = dlg.ShowDialog();
// Get the selected file name and display in a TextBox
if (result == true)
{
// Open document
string filename = dlg.FileName;
// Load the image and crop it if needed
System.Drawing.Bitmap bOrig = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(filename);
// Check aspect ratio
double cA = (bOrig.Height * 1.0) / (bOrig.Width * 1.0);
double fA = imgSnap.Height / imgSnap.Width;
System.Drawing.Bitmap img2;
if (cA > fA)
{
int idx = (bOrig.Height - Convert.ToInt32(bOrig.Width * fA)) / 2;
img2 = new System.Drawing.Bitmap(bOrig.Width, bOrig.Height - 2 * idx, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
img2.SetResolution(bOrig.HorizontalResolution, bOrig.VerticalResolution);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img2);
g.DrawImage(bOrig, 0, -idx);
}
else
{
int idx = (bOrig.Width - Convert.ToInt32(bOrig.Height / fA)) / 2;
img2 = new System.Drawing.Bitmap(bOrig.Width - 2 * idx, bOrig.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
img2.SetResolution(bOrig.HorizontalResolution, bOrig.VerticalResolution);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img2);
g.DrawImage(bOrig, -idx, 0);
}
BitmapSource bs = ImageHelper.Bitmap2BitmapSource(img2);
var bitmap = new TransformedBitmap(bs,
new ScaleTransform(imgSnap.Width / bs.PixelWidth,
imgSnap.Height / bs.PixelHeight));
fromFile = true;
imgSnap.Source = bitmap;
cropArea.Visibility = Visibility.Visible;
cam.Visibility = Visibility.Hidden;
Canvas.SetLeft(DesignerItem, (imgSnap.Width - imgSnap.Height) / 2);
Canvas.SetTop(DesignerItem, 0);
DesignerItem.Width = imgSnap.Height;
DesignerItem.Height = imgSnap.Height;
rB1_0.Visibility = Visibility.Visible;
rB1_1.Visibility = Visibility.Visible;
rB1_2.Visibility = Visibility.Visible;
rB2_0.Visibility = Visibility.Hidden;
rB2_1.Visibility = Visibility.Hidden;
rB2_2.Visibility = Visibility.Hidden;
cbRetake.Visibility = Visibility.Visible;
labB2.Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
labB1.Foreground = new SolidColorBrush(Color.FromArgb(255, 204, 204, 204));
imgB1.Source = new BitmapImage(new Uri("pack://application:,,,/Media/camera-inactive-btn.png"));
imgB2.Source = new BitmapImage(new Uri("pack://application:,,,/Media/crop-active-btn.png"));
mode = "crop";
}
else
{
cropArea.Visibility = Visibility.Hidden;
cam.VideoSourcePlayer.Start();
cam.Visibility = Visibility.Visible;
}
}
catch (Exception ex)
{
LogError(ex);
}
isBusy = false;
}
}
示例14: ChangeImageSize
//ChangeImageSize
public static System.Drawing.Image ChangeImageSize(System.Drawing.Image imgPhoto, int? Width, int? Height, bool IsEqual)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
int w, h;
if (Width.HasValue && !Height.HasValue)
{
w = Width.Value;
nPercentW = ((float)w / (float)sourceWidth);
h = (IsEqual) ? w : (int)((float)nPercentW * (float)sourceHeight);
nPercentH = ((float)h / (float)sourceHeight);
}
else if (!Width.HasValue && Height.HasValue)
{
h = Height.Value;
nPercentH = ((float)h / (float)sourceHeight);
w = (IsEqual) ? h : (int)((float)nPercentH * (float)sourceWidth);
nPercentW = ((float)w / (float)sourceWidth);
}
else
{
w = Width.Value;
h = Height.Value;
nPercentW = ((float)w / (float)sourceWidth);
nPercentH = ((float)h / (float)sourceHeight);
}
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = System.Convert.ToInt16((w - (sourceWidth * nPercent)) * 0.5);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((h - (sourceHeight * nPercent)) * 0.5);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
System.Drawing.Bitmap bmPhoto = new System.Drawing.Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.VerticalResolution, imgPhoto.HorizontalResolution);
System.Drawing.Graphics grPhoto = System.Drawing.Graphics.FromImage(bmPhoto);
grPhoto.Clear(System.Drawing.Color.White);
grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
grPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
grPhoto.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
grPhoto.DrawImage(imgPhoto, new System.Drawing.Rectangle(destX, destY, destWidth, destHeight), new System.Drawing.Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), System.Drawing.GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
示例15: HandleUpload
private void HandleUpload(FileUpload fileControl)
{
if (fileControl.HasFile)
{
try
{
Extension = fileControl.FileName.Substring(fileControl.FileName.LastIndexOf(".") + 1);
Extension = "png";
var fileName = (Server.MapPath(Folder) + FileName + "." + Extension);
lblUplderr.Text = "<br>File size: " +
fileControl.PostedFile.ContentLength + " kb<br>" +
"Content type: " +
fileControl.PostedFile.ContentType;
using (var upImage = System.Drawing.Image.FromStream(fileControl.PostedFile.InputStream))
{
// Get height and width of current image
int currWidth = (int)upImage.Width;
int currHeight = (int)upImage.Height;
Int32 newWidth = ImgWidth;
float ratio = 0;
Int32 newHeight = 0;
ratio = (float)currWidth / (float)newWidth;
newHeight = (int)(currHeight / ratio);
using (var newBmp = new System.Drawing.Bitmap(newWidth,
newHeight,
System.Drawing.Imaging.PixelFormat.Format24bppRgb))
{
newBmp.SetResolution(72, 72);
newBmp.MakeTransparent();
using (var newGraphic = System.Drawing.Graphics.FromImage(newBmp))
{
newGraphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
newGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
newGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
newGraphic.DrawImage(upImage, 0, 0, newWidth, newHeight);
newBmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
}
}
if (CreateSmallThumbnail)
{
string thumbName = string.Format("{0}\\{1}{2}.{3}",
Server.MapPath(Folder),
SmallThumbnailPrefix,
FileName,
Extension);
CreateSquareImage(upImage, thumbName, SmallThumbnailWidth);
}
if (CreateMediumThumbnail)
{
string thumbName = string.Format("{0}\\{1}{2}.{3}",
Server.MapPath(Folder),
MediumThumbnailPrefix,
FileName,
Extension);
CreateSquareImage(upImage, thumbName, MediumThumbnailWidth);
}
}
ProcessRender();
}
catch (Exception ex)
{
lblUplderr.Text = "<br><font color=red>ERROR: " + ex.Message.ToString() + "</font>";
}
}
else {
lblUplderr.Text = "<br><font color=red>ERROR: You have not specified a file.</font>";
}
}