Hi
I have below code and i want fromthe centre of Picture1 50 % should be displayed.
VB.NET
Dim lSourceX As Long, lSourceY As Long
Dim lOldWidth As Long, lOldHeight As Long
Dim lNewWidth As Long, lNewHeight As Long
Picture1.AutoRedraw = True
Picture1.Picture = LoadPicture(App.Path & "\O_" & tno & ".jpg")
Picture1.Width = Picture1.Picture.Width
Picture1.Height = Picture1.Picture.Height
Picture1.Picture = Picture1.ImageThanks

Eliana BlakeRank #1,595 · 993 reputationPosted Mar 20, 2025, 12:21 PM EDT
It seems like you are trying to display the center 50% of an image within Picture1. To achieve this, you can apply a cropping technique to focus on the central part of the image. Here's a simplified breakdown of how you could modify your code to crop and display the center 50% of the Picture1:
Dim lSourceX As Long, lSourceY As Long Dim lOldWidth As Long, lOldHeight As Long Dim lNewWidth As Long, lNewHeight As Long Picture1.AutoRedraw = True Picture1.Picture = LoadPicture(App.Path & "\O_" & tno & ".jpg") Picture1.Width = Picture1.Picture.Width Picture1.Height = Picture1.Picture.Height ' Calculate the dimensions for the cropped area (center 50%) lOldWidth = Picture1.Picture.Width lOldHeight = Picture1.Picture.Height lNewWidth = lOldWidth * 0.5 ' 50% of the image width lNewHeight = lOldHeight * 0.5 ' 50% of the image height ' Set the starting point for cropping to the center lSourceX = (lOldWidth - lNewWidth) / 2 lSourceY = (lOldHeight - lNewHeight) / 2 ' Create a new image by cropping the center 50% Set Picture1.Picture = CropImage(Picture1.Picture, lSourceX, lSourceY, lNewWidth, lNewHeight) ' Function to crop the image Function CropImage(oPic As IPictureDisp, x As Long, y As Long, w As Long, h As Long) As IPictureDisp Dim oPicNew As StdPicture Set oPicNew = New StdPicture oPicNew.Picture = oPic ' assign the original picture to the new picture ' Crop the image oPicNew.PaintPicture oPic, 0, 0, w, h, x, y, w, h ' Return the cropped image Set CropImage = oPicNew.Picture End FunctionThis code snippet enhances your existing code by adding a function `CropImage` that takes care of cropping the central 50% portion of the image loaded in Picture1. The dimensions and starting points are calculated to crop the image effectively. This implementation allows you to display the desired cropped area.
Please adjust the adjustments according to your specific requirements and the capabilities of the programming environment you are using. Let me know if you need further clarification or additional assistance!